blob: 5d41bd11081eabf8dcee70e58c82a7b452754da2 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===------------------------------ vector --------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_VECTOR
12#define _LIBCPP_VECTOR
13
14/*
15 vector synopsis
16
17namespace std
18{
19
Howard Hinnant324bb032010-08-22 00:02:4320template <class T, class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:1621class vector
Howard Hinnant324bb032010-08-22 00:02:4322{
23public:
24 typedef T value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:1625 typedef Allocator allocator_type;
26 typedef typename allocator_type::reference reference;
27 typedef typename allocator_type::const_reference const_reference;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef typename allocator_type::size_type size_type;
31 typedef typename allocator_type::difference_type difference_type;
32 typedef typename allocator_type::pointer pointer;
33 typedef typename allocator_type::const_pointer const_pointer;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36
Howard Hinnantd1d27a42011-06-03 19:40:4037 vector()
38 noexcept(is_nothrow_default_constructible<allocator_type>::value);
39 explicit vector(const allocator_type&);
Howard Hinnantbc8d3f92010-05-11 19:42:1640 explicit vector(size_type n);
Marshall Clowa49a2c92013-09-14 00:47:5941 explicit vector(size_type n, const allocator_type&); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:1642 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
43 template <class InputIterator>
44 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
45 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:4046 vector(vector&& x)
47 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1648 vector(initializer_list<value_type> il);
49 vector(initializer_list<value_type> il, const allocator_type& a);
50 ~vector();
51 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:4052 vector& operator=(vector&& x)
53 noexcept(
54 allocator_type::propagate_on_container_move_assignment::value &&
55 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:1656 vector& operator=(initializer_list<value_type> il);
57 template <class InputIterator>
58 void assign(InputIterator first, InputIterator last);
59 void assign(size_type n, const value_type& u);
60 void assign(initializer_list<value_type> il);
61
Howard Hinnantd1d27a42011-06-03 19:40:4062 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1663
Howard Hinnantd1d27a42011-06-03 19:40:4064 iterator begin() noexcept;
65 const_iterator begin() const noexcept;
66 iterator end() noexcept;
67 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1668
Howard Hinnantd1d27a42011-06-03 19:40:4069 reverse_iterator rbegin() noexcept;
70 const_reverse_iterator rbegin() const noexcept;
71 reverse_iterator rend() noexcept;
72 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1673
Howard Hinnantd1d27a42011-06-03 19:40:4074 const_iterator cbegin() const noexcept;
75 const_iterator cend() const noexcept;
76 const_reverse_iterator crbegin() const noexcept;
77 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1678
Howard Hinnantd1d27a42011-06-03 19:40:4079 size_type size() const noexcept;
80 size_type max_size() const noexcept;
81 size_type capacity() const noexcept;
82 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1683 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:4084 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1685
86 reference operator[](size_type n);
87 const_reference operator[](size_type n) const;
88 reference at(size_type n);
89 const_reference at(size_type n) const;
90
91 reference front();
92 const_reference front() const;
93 reference back();
94 const_reference back() const;
95
Howard Hinnantd1d27a42011-06-03 19:40:4096 value_type* data() noexcept;
97 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1698
99 void push_back(const value_type& x);
100 void push_back(value_type&& x);
101 template <class... Args>
102 void emplace_back(Args&&... args);
103 void pop_back();
104
105 template <class... Args> iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class InputIterator>
110 iterator insert(const_iterator position, InputIterator first, InputIterator last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator first, const_iterator last);
115
Howard Hinnantd1d27a42011-06-03 19:40:40116 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16117
118 void resize(size_type sz);
119 void resize(size_type sz, const value_type& c);
120
Howard Hinnantd1d27a42011-06-03 19:40:40121 void swap(vector&)
122 noexcept(!allocator_type::propagate_on_container_swap::value ||
123 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16124
125 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43126};
Howard Hinnantbc8d3f92010-05-11 19:42:16127
Howard Hinnant324bb032010-08-22 00:02:43128template <class Allocator = allocator<T> >
Howard Hinnantbc8d3f92010-05-11 19:42:16129class vector<bool, Allocator>
Howard Hinnant324bb032010-08-22 00:02:43130{
131public:
132 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16133 typedef Allocator allocator_type;
134 typedef implementation-defined iterator;
135 typedef implementation-defined const_iterator;
136 typedef typename allocator_type::size_type size_type;
137 typedef typename allocator_type::difference_type difference_type;
138 typedef iterator pointer;
139 typedef const_iterator const_pointer;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142
143 class reference
144 {
145 public:
Howard Hinnantd1d27a42011-06-03 19:40:40146 reference(const reference&) noexcept;
147 operator bool() const noexcept;
148 reference& operator=(const bool x) noexcept;
149 reference& operator=(const reference& x) noexcept;
150 iterator operator&() const noexcept;
151 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16152 };
153
154 class const_reference
155 {
156 public:
Howard Hinnantd1d27a42011-06-03 19:40:40157 const_reference(const reference&) noexcept;
158 operator bool() const noexcept;
159 const_iterator operator&() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16160 };
161
Howard Hinnantd1d27a42011-06-03 19:40:40162 vector()
163 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant9cbee432011-09-02 20:42:31164 explicit vector(const allocator_type&);
Marshall Clowa49a2c92013-09-14 00:47:59165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14
166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16167 template <class InputIterator>
168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type());
169 vector(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40170 vector(vector&& x)
171 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16172 vector(initializer_list<value_type> il);
173 vector(initializer_list<value_type> il, const allocator_type& a);
174 ~vector();
175 vector& operator=(const vector& x);
Howard Hinnantd1d27a42011-06-03 19:40:40176 vector& operator=(vector&& x)
177 noexcept(
178 allocator_type::propagate_on_container_move_assignment::value &&
179 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16180 vector& operator=(initializer_list<value_type> il);
181 template <class InputIterator>
182 void assign(InputIterator first, InputIterator last);
183 void assign(size_type n, const value_type& u);
184 void assign(initializer_list<value_type> il);
185
Howard Hinnantd1d27a42011-06-03 19:40:40186 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16187
Howard Hinnantd1d27a42011-06-03 19:40:40188 iterator begin() noexcept;
189 const_iterator begin() const noexcept;
190 iterator end() noexcept;
191 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16192
Howard Hinnantd1d27a42011-06-03 19:40:40193 reverse_iterator rbegin() noexcept;
194 const_reverse_iterator rbegin() const noexcept;
195 reverse_iterator rend() noexcept;
196 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16197
Howard Hinnantd1d27a42011-06-03 19:40:40198 const_iterator cbegin() const noexcept;
199 const_iterator cend() const noexcept;
200 const_reverse_iterator crbegin() const noexcept;
201 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16202
Howard Hinnantd1d27a42011-06-03 19:40:40203 size_type size() const noexcept;
204 size_type max_size() const noexcept;
205 size_type capacity() const noexcept;
206 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16207 void reserve(size_type n);
Howard Hinnantd1d27a42011-06-03 19:40:40208 void shrink_to_fit() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16209
210 reference operator[](size_type n);
211 const_reference operator[](size_type n) const;
212 reference at(size_type n);
213 const_reference at(size_type n) const;
214
215 reference front();
216 const_reference front() const;
217 reference back();
218 const_reference back() const;
219
220 void push_back(const value_type& x);
Marshall Clow198a2a52013-08-13 23:54:12221 template <class... Args> void emplace_back(Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16222 void pop_back();
223
Marshall Clow198a2a52013-08-13 23:54:12224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16225 iterator insert(const_iterator position, const value_type& x);
226 iterator insert(const_iterator position, size_type n, const value_type& x);
227 template <class InputIterator>
228 iterator insert(const_iterator position, InputIterator first, InputIterator last);
229 iterator insert(const_iterator position, initializer_list<value_type> il);
230
231 iterator erase(const_iterator position);
232 iterator erase(const_iterator first, const_iterator last);
233
Howard Hinnantd1d27a42011-06-03 19:40:40234 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16235
236 void resize(size_type sz);
237 void resize(size_type sz, value_type x);
238
Howard Hinnantd1d27a42011-06-03 19:40:40239 void swap(vector&)
240 noexcept(!allocator_type::propagate_on_container_swap::value ||
241 __is_nothrow_swappable<allocator_type>::value);
242 void flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16243
244 bool __invariants() const;
Howard Hinnant324bb032010-08-22 00:02:43245};
Howard Hinnantbc8d3f92010-05-11 19:42:16246
247template <class Allocator> struct hash<std::vector<bool, Allocator>>;
248
249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
255
Howard Hinnantd1d27a42011-06-03 19:40:40256template <class T, class Allocator>
257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
258 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16259
260} // std
261
262*/
263
264#include <__config>
265#include <__bit_reference>
266#include <type_traits>
267#include <climits>
268#include <limits>
269#include <initializer_list>
270#include <memory>
271#include <stdexcept>
272#include <algorithm>
273#include <cstring>
274#include <__split_buffer>
275#include <__functional_base>
Howard Hinnantbc8d3f92010-05-11 19:42:16276
Howard Hinnant66c6f972011-11-29 16:45:27277#include <__undef_min_max>
278
Howard Hinnant5e571422013-08-23 20:10:18279#ifdef _LIBCPP_DEBUG
Howard Hinnant8b00e6c2013-08-02 00:26:35280# include <__debug>
281#else
282# define _LIBCPP_ASSERT(x, m) ((void)0)
283#endif
284
Howard Hinnant08e17472011-10-17 20:05:10285#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16286#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10287#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16288
289_LIBCPP_BEGIN_NAMESPACE_STD
290
291template <bool>
292class __vector_base_common
293{
294protected:
295 _LIBCPP_ALWAYS_INLINE __vector_base_common() {}
296 void __throw_length_error() const;
297 void __throw_out_of_range() const;
298};
299
300template <bool __b>
301void
302__vector_base_common<__b>::__throw_length_error() const
303{
304#ifndef _LIBCPP_NO_EXCEPTIONS
305 throw length_error("vector");
306#else
307 assert(!"vector length_error");
308#endif
309}
310
311template <bool __b>
312void
313__vector_base_common<__b>::__throw_out_of_range() const
314{
315#ifndef _LIBCPP_NO_EXCEPTIONS
316 throw out_of_range("vector");
317#else
318 assert(!"vector out_of_range");
319#endif
320}
321
Howard Hinnante9df0a52013-08-01 18:17:34322#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45323#pragma warning( push )
324#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34325#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34326_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __vector_base_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34327#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45328#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34329#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16330
331template <class _Tp, class _Allocator>
332class __vector_base
333 : protected __vector_base_common<true>
334{
335protected:
Howard Hinnant324bb032010-08-22 00:02:43336 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16337 typedef _Allocator allocator_type;
338 typedef allocator_traits<allocator_type> __alloc_traits;
339 typedef value_type& reference;
340 typedef const value_type& const_reference;
341 typedef typename __alloc_traits::size_type size_type;
342 typedef typename __alloc_traits::difference_type difference_type;
343 typedef typename __alloc_traits::pointer pointer;
344 typedef typename __alloc_traits::const_pointer const_pointer;
345 typedef pointer iterator;
346 typedef const_pointer const_iterator;
347
348 pointer __begin_;
349 pointer __end_;
350 __compressed_pair<pointer, allocator_type> __end_cap_;
351
Howard Hinnantd1d27a42011-06-03 19:40:40352 _LIBCPP_INLINE_VISIBILITY
353 allocator_type& __alloc() _NOEXCEPT
354 {return __end_cap_.second();}
355 _LIBCPP_INLINE_VISIBILITY
356 const allocator_type& __alloc() const _NOEXCEPT
357 {return __end_cap_.second();}
358 _LIBCPP_INLINE_VISIBILITY
359 pointer& __end_cap() _NOEXCEPT
360 {return __end_cap_.first();}
361 _LIBCPP_INLINE_VISIBILITY
362 const pointer& __end_cap() const _NOEXCEPT
363 {return __end_cap_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16364
Howard Hinnantd1d27a42011-06-03 19:40:40365 _LIBCPP_INLINE_VISIBILITY
366 __vector_base()
367 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43368 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16369 ~__vector_base();
370
Howard Hinnantd1d27a42011-06-03 19:40:40371 _LIBCPP_INLINE_VISIBILITY
372 void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
373 _LIBCPP_INLINE_VISIBILITY
374 size_type capacity() const _NOEXCEPT
375 {return static_cast<size_type>(__end_cap() - __begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16376
Howard Hinnantd1d27a42011-06-03 19:40:40377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32378 void __destruct_at_end(pointer __new_last) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16379
Howard Hinnantee6ccd02010-09-23 18:58:28380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16381 void __copy_assign_alloc(const __vector_base& __c)
382 {__copy_assign_alloc(__c, integral_constant<bool,
383 __alloc_traits::propagate_on_container_copy_assignment::value>());}
384
Howard Hinnantee6ccd02010-09-23 18:58:28385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16386 void __move_assign_alloc(__vector_base& __c)
Howard Hinnantd1d27a42011-06-03 19:40:40387 _NOEXCEPT_(
388 !__alloc_traits::propagate_on_container_move_assignment::value ||
389 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16390 {__move_assign_alloc(__c, integral_constant<bool,
391 __alloc_traits::propagate_on_container_move_assignment::value>());}
392
Howard Hinnantee6ccd02010-09-23 18:58:28393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16394 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantd1d27a42011-06-03 19:40:40395 _NOEXCEPT_(
396 !__alloc_traits::propagate_on_container_swap::value ||
397 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16398 {__swap_alloc(__x, __y, integral_constant<bool,
399 __alloc_traits::propagate_on_container_swap::value>());}
400private:
Howard Hinnantee6ccd02010-09-23 18:58:28401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16402 void __copy_assign_alloc(const __vector_base& __c, true_type)
403 {
404 if (__alloc() != __c.__alloc())
405 {
406 clear();
407 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
408 __begin_ = __end_ = __end_cap() = nullptr;
409 }
410 __alloc() = __c.__alloc();
411 }
412
Howard Hinnantee6ccd02010-09-23 18:58:28413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04414 void __copy_assign_alloc(const __vector_base&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16415 {}
416
Howard Hinnantee6ccd02010-09-23 18:58:28417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31418 void __move_assign_alloc(__vector_base& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40419 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16420 {
Howard Hinnant0949eed2011-06-30 21:18:19421 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16422 }
423
Howard Hinnantee6ccd02010-09-23 18:58:28424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04425 void __move_assign_alloc(__vector_base&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40426 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16427 {}
428
Howard Hinnantee6ccd02010-09-23 18:58:28429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16430 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:40431 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16432 {
Howard Hinnant0949eed2011-06-30 21:18:19433 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16434 swap(__x, __y);
435 }
Howard Hinnantee6ccd02010-09-23 18:58:28436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04437 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:40438 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16439 {}
440};
441
442template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00443inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16444void
Howard Hinnant2c39cbe2013-06-27 19:35:32445__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16446{
Howard Hinnantb0bfd9b2012-02-15 00:41:34447 while (__new_last != __end_)
Howard Hinnant2c39cbe2013-06-27 19:35:32448 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16449}
450
451template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00452inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16453__vector_base<_Tp, _Allocator>::__vector_base()
Howard Hinnantd1d27a42011-06-03 19:40:40454 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:32455 : __begin_(nullptr),
456 __end_(nullptr),
457 __end_cap_(nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16458{
459}
460
461template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16463__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:32464 : __begin_(nullptr),
465 __end_(nullptr),
466 __end_cap_(nullptr, __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16467{
468}
469
470template <class _Tp, class _Allocator>
471__vector_base<_Tp, _Allocator>::~__vector_base()
472{
Howard Hinnant2c39cbe2013-06-27 19:35:32473 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16474 {
475 clear();
476 __alloc_traits::deallocate(__alloc(), __begin_, capacity());
477 }
478}
479
480template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34481class _LIBCPP_TYPE_VIS_ONLY vector
Howard Hinnantbc8d3f92010-05-11 19:42:16482 : private __vector_base<_Tp, _Allocator>
483{
484private:
485 typedef __vector_base<_Tp, _Allocator> __base;
Marshall Clow1f50f2d2014-05-08 14:14:06486 typedef allocator<_Tp> __default_allocator_type;
Howard Hinnant324bb032010-08-22 00:02:43487public:
Howard Hinnantbc8d3f92010-05-11 19:42:16488 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:43489 typedef _Tp value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16490 typedef _Allocator allocator_type;
491 typedef typename __base::__alloc_traits __alloc_traits;
492 typedef typename __base::reference reference;
493 typedef typename __base::const_reference const_reference;
494 typedef typename __base::size_type size_type;
495 typedef typename __base::difference_type difference_type;
496 typedef typename __base::pointer pointer;
497 typedef typename __base::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16498 typedef __wrap_iter<pointer> iterator;
499 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19500 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
501 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16502
Howard Hinnant02d5e182013-03-26 19:04:56503 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
504 "Allocator::value_type must be same type as value_type");
505
Howard Hinnantd1d27a42011-06-03 19:40:40506 _LIBCPP_INLINE_VISIBILITY
507 vector()
508 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant7a563db2011-09-14 18:33:51509 {
Howard Hinnantabe26282011-09-16 17:29:17510#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51511 __get_db()->__insert_c(this);
512#endif
513 }
514 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
515 : __base(__a)
516 {
Howard Hinnantabe26282011-09-16 17:29:17517#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51518 __get_db()->__insert_c(this);
519#endif
520 }
Howard Hinnantbc8d3f92010-05-11 19:42:16521 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:59522#if _LIBCPP_STD_VER > 11
523 explicit vector(size_type __n, const allocator_type& __a);
524#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16525 vector(size_type __n, const_reference __x);
526 vector(size_type __n, const_reference __x, const allocator_type& __a);
527 template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:54528 vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:16529 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32530 !__is_forward_iterator<_InputIterator>::value &&
531 is_constructible<
532 value_type,
Howard Hinnantde589f22013-09-21 21:13:54533 typename iterator_traits<_InputIterator>::reference>::value,
534 _InputIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16535 template <class _InputIterator>
536 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
537 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32538 !__is_forward_iterator<_InputIterator>::value &&
539 is_constructible<
540 value_type,
541 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16542 template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:54543 vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:32544 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
545 is_constructible<
546 value_type,
Howard Hinnantde589f22013-09-21 21:13:54547 typename iterator_traits<_ForwardIterator>::reference>::value,
548 _ForwardIterator>::type __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16549 template <class _ForwardIterator>
550 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:32551 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
552 is_constructible<
553 value_type,
554 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02555#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16557 vector(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16559 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02560#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantabe26282011-09-16 17:29:17561#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantee6ccd02010-09-23 18:58:28562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7a563db2011-09-14 18:33:51563 ~vector()
564 {
565 __get_db()->__erase_c(this);
566 }
Howard Hinnantbc8d3f92010-05-11 19:42:16567#endif
568
569 vector(const vector& __x);
570 vector(const vector& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16572 vector& operator=(const vector& __x);
Howard Hinnant73d21a42010-09-04 23:28:19573#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40575 vector(vector&& __x)
576 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16578 vector(vector&& __x, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40580 vector& operator=(vector&& __x)
581 _NOEXCEPT_(
582 __alloc_traits::propagate_on_container_move_assignment::value &&
583 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19584#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02585#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16587 vector& operator=(initializer_list<value_type> __il)
588 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02589#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16590
591 template <class _InputIterator>
592 typename enable_if
593 <
594 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32595 !__is_forward_iterator<_InputIterator>::value &&
596 is_constructible<
597 value_type,
598 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16599 void
600 >::type
601 assign(_InputIterator __first, _InputIterator __last);
602 template <class _ForwardIterator>
603 typename enable_if
604 <
Howard Hinnant742fecb2013-03-28 17:44:32605 __is_forward_iterator<_ForwardIterator>::value &&
606 is_constructible<
607 value_type,
608 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16609 void
610 >::type
611 assign(_ForwardIterator __first, _ForwardIterator __last);
612
613 void assign(size_type __n, const_reference __u);
Howard Hinnante3e32912011-08-12 21:56:02614#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16616 void assign(initializer_list<value_type> __il)
617 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02618#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16619
Howard Hinnantd1d27a42011-06-03 19:40:40620 _LIBCPP_INLINE_VISIBILITY
621 allocator_type get_allocator() const _NOEXCEPT
622 {return this->__alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16623
Howard Hinnantd1d27a42011-06-03 19:40:40624 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT;
625 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT;
626 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT;
627 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16628
Howard Hinnantd1d27a42011-06-03 19:40:40629 _LIBCPP_INLINE_VISIBILITY
630 reverse_iterator rbegin() _NOEXCEPT
631 {return reverse_iterator(end());}
632 _LIBCPP_INLINE_VISIBILITY
633 const_reverse_iterator rbegin() const _NOEXCEPT
634 {return const_reverse_iterator(end());}
635 _LIBCPP_INLINE_VISIBILITY
636 reverse_iterator rend() _NOEXCEPT
637 {return reverse_iterator(begin());}
638 _LIBCPP_INLINE_VISIBILITY
639 const_reverse_iterator rend() const _NOEXCEPT
640 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16641
Howard Hinnantd1d27a42011-06-03 19:40:40642 _LIBCPP_INLINE_VISIBILITY
643 const_iterator cbegin() const _NOEXCEPT
644 {return begin();}
645 _LIBCPP_INLINE_VISIBILITY
646 const_iterator cend() const _NOEXCEPT
647 {return end();}
648 _LIBCPP_INLINE_VISIBILITY
649 const_reverse_iterator crbegin() const _NOEXCEPT
650 {return rbegin();}
651 _LIBCPP_INLINE_VISIBILITY
652 const_reverse_iterator crend() const _NOEXCEPT
653 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16654
Howard Hinnantd1d27a42011-06-03 19:40:40655 _LIBCPP_INLINE_VISIBILITY
656 size_type size() const _NOEXCEPT
657 {return static_cast<size_type>(this->__end_ - this->__begin_);}
658 _LIBCPP_INLINE_VISIBILITY
659 size_type capacity() const _NOEXCEPT
660 {return __base::capacity();}
661 _LIBCPP_INLINE_VISIBILITY
662 bool empty() const _NOEXCEPT
663 {return this->__begin_ == this->__end_;}
664 size_type max_size() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16665 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40666 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16667
668 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n);
669 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const;
670 reference at(size_type __n);
671 const_reference at(size_type __n) const;
672
Howard Hinnant7a563db2011-09-14 18:33:51673 _LIBCPP_INLINE_VISIBILITY reference front()
674 {
675 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
676 return *this->__begin_;
677 }
678 _LIBCPP_INLINE_VISIBILITY const_reference front() const
679 {
680 _LIBCPP_ASSERT(!empty(), "front() called for empty vector");
681 return *this->__begin_;
682 }
683 _LIBCPP_INLINE_VISIBILITY reference back()
684 {
685 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
686 return *(this->__end_ - 1);
687 }
688 _LIBCPP_INLINE_VISIBILITY const_reference back() const
689 {
690 _LIBCPP_ASSERT(!empty(), "back() called for empty vector");
691 return *(this->__end_ - 1);
692 }
Howard Hinnantbc8d3f92010-05-11 19:42:16693
Howard Hinnantd1d27a42011-06-03 19:40:40694 _LIBCPP_INLINE_VISIBILITY
695 value_type* data() _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19696 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantd1d27a42011-06-03 19:40:40697 _LIBCPP_INLINE_VISIBILITY
698 const value_type* data() const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19699 {return _VSTD::__to_raw_pointer(this->__begin_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16700
Howard Hinnant2d72b1e2010-12-17 14:46:43701 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19702#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantb0bfd9b2012-02-15 00:41:34703 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19704#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16705 template <class... _Args>
706 void emplace_back(_Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19707#endif // _LIBCPP_HAS_NO_VARIADICS
708#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16709 void pop_back();
710
711 iterator insert(const_iterator __position, const_reference __x);
Howard Hinnant73d21a42010-09-04 23:28:19712#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16713 iterator insert(const_iterator __position, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19714#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16715 template <class... _Args>
716 iterator emplace(const_iterator __position, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19717#endif // _LIBCPP_HAS_NO_VARIADICS
718#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16719 iterator insert(const_iterator __position, size_type __n, const_reference __x);
720 template <class _InputIterator>
721 typename enable_if
722 <
723 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:32724 !__is_forward_iterator<_InputIterator>::value &&
725 is_constructible<
726 value_type,
727 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16728 iterator
729 >::type
730 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
731 template <class _ForwardIterator>
732 typename enable_if
733 <
Howard Hinnant742fecb2013-03-28 17:44:32734 __is_forward_iterator<_ForwardIterator>::value &&
735 is_constructible<
736 value_type,
737 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16738 iterator
739 >::type
740 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02741#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:28742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16743 iterator insert(const_iterator __position, initializer_list<value_type> __il)
744 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02745#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16746
Howard Hinnant2d72b1e2010-12-17 14:46:43747 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:16748 iterator erase(const_iterator __first, const_iterator __last);
749
Howard Hinnantd1d27a42011-06-03 19:40:40750 _LIBCPP_INLINE_VISIBILITY
751 void clear() _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51752 {
Marshall Clow1f50f2d2014-05-08 14:14:06753 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51754 __base::clear();
Marshall Clow1f50f2d2014-05-08 14:14:06755 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51756 __invalidate_all_iterators();
757 }
Howard Hinnantbc8d3f92010-05-11 19:42:16758
759 void resize(size_type __sz);
760 void resize(size_type __sz, const_reference __x);
761
Howard Hinnantd1d27a42011-06-03 19:40:40762 void swap(vector&)
763 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
764 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16765
766 bool __invariants() const;
767
Howard Hinnantabe26282011-09-16 17:29:17768#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51769
770 bool __dereferenceable(const const_iterator* __i) const;
771 bool __decrementable(const const_iterator* __i) const;
772 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
773 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
774
Howard Hinnantabe26282011-09-16 17:29:17775#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51776
Howard Hinnantbc8d3f92010-05-11 19:42:16777private:
Howard Hinnant2d72b1e2010-12-17 14:46:43778 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:16779 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:40780 void deallocate() _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43781 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
Howard Hinnant04240d92011-01-04 19:53:31782 void __construct_at_end(size_type __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16783 void __construct_at_end(size_type __n, const_reference __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16784 template <class _ForwardIterator>
785 typename enable_if
786 <
787 __is_forward_iterator<_ForwardIterator>::value,
788 void
789 >::type
790 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
791 void __move_construct_at_end(pointer __first, pointer __last);
792 void __append(size_type __n);
793 void __append(size_type __n, const_reference __x);
Howard Hinnant2d72b1e2010-12-17 14:46:43794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40795 iterator __make_iter(pointer __p) _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:40797 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16798 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
799 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
800 void __move_range(pointer __from_s, pointer __from_e, pointer __to);
Howard Hinnantd1d27a42011-06-03 19:40:40801 void __move_assign(vector& __c, true_type)
802 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16803 void __move_assign(vector& __c, false_type);
Howard Hinnant7a563db2011-09-14 18:33:51804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2c39cbe2013-06-27 19:35:32805 void __destruct_at_end(pointer __new_last) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51806 {
Howard Hinnantabe26282011-09-16 17:29:17807#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:51808 __c_node* __c = __get_db()->__find_c_and_lock(this);
809 for (__i_node** __p = __c->end_; __p != __c->beg_; )
810 {
811 --__p;
812 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
813 if (__i->base() > __new_last)
814 {
815 (*__p)->__c_ = nullptr;
816 if (--__c->end_ != __p)
817 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
818 }
819 }
820 __get_db()->unlock();
821#endif
Marshall Clow1f50f2d2014-05-08 14:14:06822 size_type __old_size = size();
Howard Hinnant7a563db2011-09-14 18:33:51823 __base::__destruct_at_end(__new_last);
Marshall Clow1f50f2d2014-05-08 14:14:06824 __annotate_shrink(__old_size);
Howard Hinnant7a563db2011-09-14 18:33:51825 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34826 template <class _Up>
827 void
828#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
829 __push_back_slow_path(_Up&& __x);
830#else
831 __push_back_slow_path(_Up& __x);
832#endif
Howard Hinnant0438ea22012-02-26 15:30:12833#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
834 template <class... _Args>
835 void
836 __emplace_back_slow_path(_Args&&... __args);
837#endif
Marshall Clow1f50f2d2014-05-08 14:14:06838 // The following functions are no-ops outside of AddressSanitizer mode.
839 // We call annotatations only for the default Allocator because other allocators
840 // may not meet the AddressSanitizer alignment constraints.
841 // See the documentation for __sanitizer_annotate_contiguous_container for more details.
842 void __annotate_contiguous_container
843 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid)
844 {
845#ifndef _LIBCPP_HAS_NO_ASAN
846 if (__beg && is_same<allocator_type, __default_allocator_type>::value)
847 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
848#endif
849 }
850
851 void __annotate_new(size_type __current_size)
852 {
853 __annotate_contiguous_container(data(), data() + capacity(),
854 data() + capacity(), data() + __current_size);
855 }
856 void __annotate_delete()
857 {
858 __annotate_contiguous_container(data(), data() + capacity(),
859 data() + size(), data() + capacity());
860 }
861 void __annotate_increase(size_type __n)
862 {
863 __annotate_contiguous_container(data(), data() + capacity(),
864 data() + size(), data() + size() + __n);
865 }
866 void __annotate_shrink(size_type __old_size)
867 {
868 __annotate_contiguous_container(data(), data() + capacity(),
869 data() + __old_size, data() + size());
870 }
Howard Hinnantbc8d3f92010-05-11 19:42:16871};
872
873template <class _Tp, class _Allocator>
874void
875vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v)
876{
Marshall Clow1f50f2d2014-05-08 14:14:06877 __annotate_delete();
Howard Hinnantb0bfd9b2012-02-15 00:41:34878 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
Howard Hinnant0949eed2011-06-30 21:18:19879 _VSTD::swap(this->__begin_, __v.__begin_);
880 _VSTD::swap(this->__end_, __v.__end_);
881 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16882 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06883 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16884 __invalidate_all_iterators();
885}
886
887template <class _Tp, class _Allocator>
888typename vector<_Tp, _Allocator>::pointer
889vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p)
890{
Marshall Clow1f50f2d2014-05-08 14:14:06891 __annotate_delete();
Howard Hinnantbc8d3f92010-05-11 19:42:16892 pointer __r = __v.__begin_;
Howard Hinnantb0bfd9b2012-02-15 00:41:34893 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_);
894 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_);
Howard Hinnant0949eed2011-06-30 21:18:19895 _VSTD::swap(this->__begin_, __v.__begin_);
896 _VSTD::swap(this->__end_, __v.__end_);
897 _VSTD::swap(this->__end_cap(), __v.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16898 __v.__first_ = __v.__begin_;
Marshall Clow1f50f2d2014-05-08 14:14:06899 __annotate_new(size());
Howard Hinnantbc8d3f92010-05-11 19:42:16900 __invalidate_all_iterators();
901 return __r;
902}
903
904// Allocate space for __n objects
905// throws length_error if __n > max_size()
906// throws (probably bad_alloc) if memory run out
907// Precondition: __begin_ == __end_ == __end_cap() == 0
908// Precondition: __n > 0
909// Postcondition: capacity() == __n
910// Postcondition: size() == 0
911template <class _Tp, class _Allocator>
912void
913vector<_Tp, _Allocator>::allocate(size_type __n)
914{
915 if (__n > max_size())
916 this->__throw_length_error();
917 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n);
918 this->__end_cap() = this->__begin_ + __n;
Marshall Clow1f50f2d2014-05-08 14:14:06919 __annotate_new(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16920}
921
922template <class _Tp, class _Allocator>
923void
Howard Hinnantd1d27a42011-06-03 19:40:40924vector<_Tp, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16925{
Howard Hinnant2c39cbe2013-06-27 19:35:32926 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16927 {
928 clear();
929 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
Howard Hinnant2c39cbe2013-06-27 19:35:32930 this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16931 }
932}
933
934template <class _Tp, class _Allocator>
935typename vector<_Tp, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:40936vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16937{
Sean Hunt110b8bf2011-07-29 23:31:58938 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always
Howard Hinnantbc8d3f92010-05-11 19:42:16939}
940
941// Precondition: __new_size > capacity()
942template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00943inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16944typename vector<_Tp, _Allocator>::size_type
945vector<_Tp, _Allocator>::__recommend(size_type __new_size) const
946{
947 const size_type __ms = max_size();
948 if (__new_size > __ms)
949 this->__throw_length_error();
950 const size_type __cap = capacity();
951 if (__cap >= __ms / 2)
952 return __ms;
Sean Hunt110b8bf2011-07-29 23:31:58953 return _VSTD::max<size_type>(2*__cap, __new_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16954}
955
956// Default constructs __n objects starting at __end_
957// throws if construction throws
958// Precondition: __n > 0
959// Precondition: size() + __n <= capacity()
960// Postcondition: size() == size() + __n
961template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16962void
963vector<_Tp, _Allocator>::__construct_at_end(size_type __n)
964{
Howard Hinnantbc8d3f92010-05-11 19:42:16965 allocator_type& __a = this->__alloc();
Marshall Clow1f50f2d2014-05-08 14:14:06966 __annotate_increase(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16967 do
968 {
Howard Hinnant0949eed2011-06-30 21:18:19969 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
Howard Hinnantbc8d3f92010-05-11 19:42:16970 ++this->__end_;
971 --__n;
972 } while (__n > 0);
973}
974
Howard Hinnantbc8d3f92010-05-11 19:42:16975// Copy constructs __n objects starting at __end_ from __x
976// throws if construction throws
977// Precondition: __n > 0
978// Precondition: size() + __n <= capacity()
979// Postcondition: size() == old size() + __n
980// Postcondition: [i] == __x for all i in [size() - __n, __n)
981template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00982inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16983void
984vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
985{
Howard Hinnantbc8d3f92010-05-11 19:42:16986 allocator_type& __a = this->__alloc();
Marshall Clow1f50f2d2014-05-08 14:14:06987 __annotate_increase(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16988 do
989 {
Howard Hinnant0949eed2011-06-30 21:18:19990 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16991 ++this->__end_;
992 --__n;
993 } while (__n > 0);
994}
995
996template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16997template <class _ForwardIterator>
998typename enable_if
999<
1000 __is_forward_iterator<_ForwardIterator>::value,
1001 void
1002>::type
1003vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
1004{
1005 allocator_type& __a = this->__alloc();
1006 for (; __first != __last; ++__first)
1007 {
Marshall Clow1f50f2d2014-05-08 14:14:061008 __annotate_increase(1);
Howard Hinnant0949eed2011-06-30 21:18:191009 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:161010 ++this->__end_;
1011 }
1012}
1013
1014template <class _Tp, class _Allocator>
1015void
1016vector<_Tp, _Allocator>::__move_construct_at_end(pointer __first, pointer __last)
1017{
1018 allocator_type& __a = this->__alloc();
1019 for (; __first != __last; ++__first)
1020 {
Marshall Clow1f50f2d2014-05-08 14:14:061021 __annotate_increase(1);
Howard Hinnant0949eed2011-06-30 21:18:191022 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
1023 _VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:161024 ++this->__end_;
1025 }
1026}
1027
1028// Default constructs __n objects starting at __end_
1029// throws if construction throws
1030// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:401031// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:161032template <class _Tp, class _Allocator>
1033void
1034vector<_Tp, _Allocator>::__append(size_type __n)
1035{
1036 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1037 this->__construct_at_end(__n);
1038 else
1039 {
1040 allocator_type& __a = this->__alloc();
1041 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1042 __v.__construct_at_end(__n);
1043 __swap_out_circular_buffer(__v);
1044 }
1045}
1046
1047// Default constructs __n objects starting at __end_
1048// throws if construction throws
1049// Postcondition: size() == size() + __n
Howard Hinnantd1d27a42011-06-03 19:40:401050// Exception safety: strong.
Howard Hinnantbc8d3f92010-05-11 19:42:161051template <class _Tp, class _Allocator>
1052void
1053vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x)
1054{
1055 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n)
1056 this->__construct_at_end(__n, __x);
1057 else
1058 {
1059 allocator_type& __a = this->__alloc();
1060 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a);
1061 __v.__construct_at_end(__n, __x);
1062 __swap_out_circular_buffer(__v);
1063 }
1064}
1065
1066template <class _Tp, class _Allocator>
1067vector<_Tp, _Allocator>::vector(size_type __n)
1068{
Howard Hinnant0442b122011-09-16 18:41:291069#if _LIBCPP_DEBUG_LEVEL >= 2
1070 __get_db()->__insert_c(this);
1071#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161072 if (__n > 0)
1073 {
1074 allocate(__n);
1075 __construct_at_end(__n);
1076 }
1077}
1078
Marshall Clowa49a2c92013-09-14 00:47:591079#if _LIBCPP_STD_VER > 11
1080template <class _Tp, class _Allocator>
1081vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a)
1082 : __base(__a)
1083{
1084#if _LIBCPP_DEBUG_LEVEL >= 2
1085 __get_db()->__insert_c(this);
1086#endif
1087 if (__n > 0)
1088 {
1089 allocate(__n);
1090 __construct_at_end(__n);
1091 }
1092}
1093#endif
1094
Howard Hinnantbc8d3f92010-05-11 19:42:161095template <class _Tp, class _Allocator>
1096vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x)
1097{
Howard Hinnant0442b122011-09-16 18:41:291098#if _LIBCPP_DEBUG_LEVEL >= 2
1099 __get_db()->__insert_c(this);
1100#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161101 if (__n > 0)
1102 {
1103 allocate(__n);
1104 __construct_at_end(__n, __x);
1105 }
1106}
1107
1108template <class _Tp, class _Allocator>
1109vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a)
1110 : __base(__a)
1111{
Howard Hinnant0442b122011-09-16 18:41:291112#if _LIBCPP_DEBUG_LEVEL >= 2
1113 __get_db()->__insert_c(this);
1114#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161115 if (__n > 0)
1116 {
1117 allocate(__n);
1118 __construct_at_end(__n, __x);
1119 }
1120}
1121
1122template <class _Tp, class _Allocator>
1123template <class _InputIterator>
Howard Hinnantde589f22013-09-21 21:13:541124vector<_Tp, _Allocator>::vector(_InputIterator __first,
Howard Hinnantbc8d3f92010-05-11 19:42:161125 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321126 !__is_forward_iterator<_InputIterator>::value &&
1127 is_constructible<
1128 value_type,
Howard Hinnantde589f22013-09-21 21:13:541129 typename iterator_traits<_InputIterator>::reference>::value,
1130 _InputIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:161131{
Howard Hinnantabe26282011-09-16 17:29:171132#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511133 __get_db()->__insert_c(this);
1134#endif
Howard Hinnant0442b122011-09-16 18:41:291135 for (; __first != __last; ++__first)
1136 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:161137}
1138
1139template <class _Tp, class _Allocator>
1140template <class _InputIterator>
1141vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
1142 typename enable_if<__is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321143 !__is_forward_iterator<_InputIterator>::value &&
1144 is_constructible<
1145 value_type,
1146 typename iterator_traits<_InputIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:161147 : __base(__a)
1148{
Howard Hinnantabe26282011-09-16 17:29:171149#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511150 __get_db()->__insert_c(this);
1151#endif
Howard Hinnant0442b122011-09-16 18:41:291152 for (; __first != __last; ++__first)
1153 push_back(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:161154}
1155
1156template <class _Tp, class _Allocator>
1157template <class _ForwardIterator>
Howard Hinnantde589f22013-09-21 21:13:541158vector<_Tp, _Allocator>::vector(_ForwardIterator __first,
Howard Hinnant742fecb2013-03-28 17:44:321159 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1160 is_constructible<
1161 value_type,
Howard Hinnantde589f22013-09-21 21:13:541162 typename iterator_traits<_ForwardIterator>::reference>::value,
1163 _ForwardIterator>::type __last)
Howard Hinnantbc8d3f92010-05-11 19:42:161164{
Howard Hinnant0442b122011-09-16 18:41:291165#if _LIBCPP_DEBUG_LEVEL >= 2
1166 __get_db()->__insert_c(this);
1167#endif
Howard Hinnant0949eed2011-06-30 21:18:191168 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:161169 if (__n > 0)
1170 {
1171 allocate(__n);
1172 __construct_at_end(__first, __last);
1173 }
1174}
1175
1176template <class _Tp, class _Allocator>
1177template <class _ForwardIterator>
1178vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
Howard Hinnant742fecb2013-03-28 17:44:321179 typename enable_if<__is_forward_iterator<_ForwardIterator>::value &&
1180 is_constructible<
1181 value_type,
1182 typename iterator_traits<_ForwardIterator>::reference>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:161183 : __base(__a)
1184{
Howard Hinnant0442b122011-09-16 18:41:291185#if _LIBCPP_DEBUG_LEVEL >= 2
1186 __get_db()->__insert_c(this);
1187#endif
Howard Hinnant0949eed2011-06-30 21:18:191188 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:161189 if (__n > 0)
1190 {
1191 allocate(__n);
1192 __construct_at_end(__first, __last);
1193 }
1194}
1195
1196template <class _Tp, class _Allocator>
1197vector<_Tp, _Allocator>::vector(const vector& __x)
1198 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
1199{
Howard Hinnant0442b122011-09-16 18:41:291200#if _LIBCPP_DEBUG_LEVEL >= 2
1201 __get_db()->__insert_c(this);
1202#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161203 size_type __n = __x.size();
1204 if (__n > 0)
1205 {
1206 allocate(__n);
1207 __construct_at_end(__x.__begin_, __x.__end_);
1208 }
1209}
1210
1211template <class _Tp, class _Allocator>
1212vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a)
1213 : __base(__a)
1214{
Howard Hinnant0442b122011-09-16 18:41:291215#if _LIBCPP_DEBUG_LEVEL >= 2
1216 __get_db()->__insert_c(this);
1217#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161218 size_type __n = __x.size();
1219 if (__n > 0)
1220 {
1221 allocate(__n);
1222 __construct_at_end(__x.__begin_, __x.__end_);
1223 }
1224}
1225
Howard Hinnant73d21a42010-09-04 23:28:191226#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161227
1228template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161230vector<_Tp, _Allocator>::vector(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:401231 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:191232 : __base(_VSTD::move(__x.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:161233{
Howard Hinnantabe26282011-09-16 17:29:171234#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511235 __get_db()->__insert_c(this);
Howard Hinnante6125bd2011-09-19 16:34:291236 __get_db()->swap(this, &__x);
Howard Hinnant7a563db2011-09-14 18:33:511237#endif
Howard Hinnant0442b122011-09-16 18:41:291238 this->__begin_ = __x.__begin_;
1239 this->__end_ = __x.__end_;
1240 this->__end_cap() = __x.__end_cap();
Howard Hinnant2c39cbe2013-06-27 19:35:321241 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:161242}
1243
1244template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161246vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a)
1247 : __base(__a)
1248{
Howard Hinnant0442b122011-09-16 18:41:291249#if _LIBCPP_DEBUG_LEVEL >= 2
1250 __get_db()->__insert_c(this);
1251#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161252 if (__a == __x.__alloc())
1253 {
1254 this->__begin_ = __x.__begin_;
1255 this->__end_ = __x.__end_;
1256 this->__end_cap() = __x.__end_cap();
1257 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:291258#if _LIBCPP_DEBUG_LEVEL >= 2
1259 __get_db()->swap(this, &__x);
1260#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161261 }
1262 else
1263 {
Howard Hinnant99968442011-11-29 18:15:501264 typedef move_iterator<iterator> _Ip;
1265 assign(_Ip(__x.begin()), _Ip(__x.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161266 }
1267}
1268
Howard Hinnante3e32912011-08-12 21:56:021269#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1270
Howard Hinnantbc8d3f92010-05-11 19:42:161271template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001272inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161273vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
1274{
Howard Hinnant0442b122011-09-16 18:41:291275#if _LIBCPP_DEBUG_LEVEL >= 2
1276 __get_db()->__insert_c(this);
1277#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161278 if (__il.size() > 0)
1279 {
1280 allocate(__il.size());
1281 __construct_at_end(__il.begin(), __il.end());
1282 }
1283}
1284
1285template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001286inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161287vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
1288 : __base(__a)
1289{
Howard Hinnant0442b122011-09-16 18:41:291290#if _LIBCPP_DEBUG_LEVEL >= 2
1291 __get_db()->__insert_c(this);
1292#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161293 if (__il.size() > 0)
1294 {
1295 allocate(__il.size());
1296 __construct_at_end(__il.begin(), __il.end());
1297 }
1298}
1299
Howard Hinnante3e32912011-08-12 21:56:021300#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1301
Howard Hinnantbc8d3f92010-05-11 19:42:161302template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161304vector<_Tp, _Allocator>&
1305vector<_Tp, _Allocator>::operator=(vector&& __x)
Howard Hinnantd1d27a42011-06-03 19:40:401306 _NOEXCEPT_(
1307 __alloc_traits::propagate_on_container_move_assignment::value &&
1308 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161309{
1310 __move_assign(__x, integral_constant<bool,
1311 __alloc_traits::propagate_on_container_move_assignment::value>());
1312 return *this;
1313}
1314
1315template <class _Tp, class _Allocator>
1316void
1317vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
1318{
1319 if (__base::__alloc() != __c.__alloc())
1320 {
Howard Hinnant99968442011-11-29 18:15:501321 typedef move_iterator<iterator> _Ip;
1322 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161323 }
1324 else
1325 __move_assign(__c, true_type());
1326}
1327
1328template <class _Tp, class _Allocator>
1329void
1330vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:401331 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161332{
1333 deallocate();
Marshall Clow3c2eac62014-07-21 15:11:131334 __base::__move_assign_alloc(__c); // this can throw
Howard Hinnantbc8d3f92010-05-11 19:42:161335 this->__begin_ = __c.__begin_;
1336 this->__end_ = __c.__end_;
1337 this->__end_cap() = __c.__end_cap();
Howard Hinnantbc8d3f92010-05-11 19:42:161338 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
Howard Hinnante6125bd2011-09-19 16:34:291339#if _LIBCPP_DEBUG_LEVEL >= 2
1340 __get_db()->swap(this, &__c);
1341#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161342}
1343
Howard Hinnant73d21a42010-09-04 23:28:191344#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161345
1346template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001347inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161348vector<_Tp, _Allocator>&
1349vector<_Tp, _Allocator>::operator=(const vector& __x)
1350{
1351 if (this != &__x)
1352 {
1353 __base::__copy_assign_alloc(__x);
1354 assign(__x.__begin_, __x.__end_);
1355 }
1356 return *this;
1357}
1358
1359template <class _Tp, class _Allocator>
1360template <class _InputIterator>
1361typename enable_if
1362<
1363 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321364 !__is_forward_iterator<_InputIterator>::value &&
1365 is_constructible<
1366 _Tp,
1367 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161368 void
1369>::type
1370vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1371{
1372 clear();
1373 for (; __first != __last; ++__first)
1374 push_back(*__first);
1375}
1376
1377template <class _Tp, class _Allocator>
1378template <class _ForwardIterator>
1379typename enable_if
1380<
Howard Hinnant742fecb2013-03-28 17:44:321381 __is_forward_iterator<_ForwardIterator>::value &&
1382 is_constructible<
1383 _Tp,
1384 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161385 void
1386>::type
1387vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1388{
Howard Hinnant0949eed2011-06-30 21:18:191389 typename iterator_traits<_ForwardIterator>::difference_type __new_size = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:161390 if (static_cast<size_type>(__new_size) <= capacity())
1391 {
1392 _ForwardIterator __mid = __last;
1393 bool __growing = false;
1394 if (static_cast<size_type>(__new_size) > size())
1395 {
1396 __growing = true;
1397 __mid = __first;
Howard Hinnant0949eed2011-06-30 21:18:191398 _VSTD::advance(__mid, size());
Howard Hinnantbc8d3f92010-05-11 19:42:161399 }
Howard Hinnant0949eed2011-06-30 21:18:191400 pointer __m = _VSTD::copy(__first, __mid, this->__begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:161401 if (__growing)
1402 __construct_at_end(__mid, __last);
1403 else
1404 this->__destruct_at_end(__m);
1405 }
1406 else
1407 {
1408 deallocate();
1409 allocate(__recommend(static_cast<size_type>(__new_size)));
1410 __construct_at_end(__first, __last);
1411 }
1412}
1413
1414template <class _Tp, class _Allocator>
1415void
1416vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u)
1417{
1418 if (__n <= capacity())
1419 {
1420 size_type __s = size();
Howard Hinnant0949eed2011-06-30 21:18:191421 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u);
Howard Hinnantbc8d3f92010-05-11 19:42:161422 if (__n > __s)
1423 __construct_at_end(__n - __s, __u);
1424 else
Howard Hinnantadff4892010-05-24 17:49:411425 this->__destruct_at_end(this->__begin_ + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:161426 }
1427 else
1428 {
1429 deallocate();
1430 allocate(__recommend(static_cast<size_type>(__n)));
1431 __construct_at_end(__n, __u);
1432 }
1433}
1434
Howard Hinnant324bb032010-08-22 00:02:431435template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001436inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161437typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401438vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161439{
Howard Hinnantabe26282011-09-16 17:29:171440#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:161441 return iterator(this, __p);
1442#else
1443 return iterator(__p);
1444#endif
1445}
1446
Howard Hinnant324bb032010-08-22 00:02:431447template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001448inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161449typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401450vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161451{
Howard Hinnantabe26282011-09-16 17:29:171452#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:161453 return const_iterator(this, __p);
1454#else
1455 return const_iterator(__p);
1456#endif
1457}
1458
Howard Hinnant324bb032010-08-22 00:02:431459template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001460inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161461typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401462vector<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161463{
1464 return __make_iter(this->__begin_);
1465}
1466
Howard Hinnant324bb032010-08-22 00:02:431467template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001468inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161469typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401470vector<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161471{
1472 return __make_iter(this->__begin_);
1473}
1474
Howard Hinnant324bb032010-08-22 00:02:431475template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001476inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161477typename vector<_Tp, _Allocator>::iterator
Howard Hinnantd1d27a42011-06-03 19:40:401478vector<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161479{
1480 return __make_iter(this->__end_);
1481}
1482
Howard Hinnant324bb032010-08-22 00:02:431483template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001484inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161485typename vector<_Tp, _Allocator>::const_iterator
Howard Hinnantd1d27a42011-06-03 19:40:401486vector<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161487{
1488 return __make_iter(this->__end_);
1489}
1490
Howard Hinnant324bb032010-08-22 00:02:431491template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001492inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161493typename vector<_Tp, _Allocator>::reference
1494vector<_Tp, _Allocator>::operator[](size_type __n)
1495{
Howard Hinnant7a563db2011-09-14 18:33:511496 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:161497 return this->__begin_[__n];
1498}
1499
Howard Hinnant324bb032010-08-22 00:02:431500template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001501inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161502typename vector<_Tp, _Allocator>::const_reference
1503vector<_Tp, _Allocator>::operator[](size_type __n) const
1504{
Howard Hinnant7a563db2011-09-14 18:33:511505 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:161506 return this->__begin_[__n];
1507}
1508
Howard Hinnant324bb032010-08-22 00:02:431509template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161510typename vector<_Tp, _Allocator>::reference
1511vector<_Tp, _Allocator>::at(size_type __n)
1512{
1513 if (__n >= size())
1514 this->__throw_out_of_range();
1515 return this->__begin_[__n];
1516}
1517
Howard Hinnant324bb032010-08-22 00:02:431518template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:161519typename vector<_Tp, _Allocator>::const_reference
1520vector<_Tp, _Allocator>::at(size_type __n) const
1521{
1522 if (__n >= size())
1523 this->__throw_out_of_range();
1524 return this->__begin_[__n];
1525}
1526
1527template <class _Tp, class _Allocator>
1528void
1529vector<_Tp, _Allocator>::reserve(size_type __n)
1530{
1531 if (__n > capacity())
1532 {
1533 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:401534 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:161535 __swap_out_circular_buffer(__v);
1536 }
1537}
1538
1539template <class _Tp, class _Allocator>
1540void
Howard Hinnantd1d27a42011-06-03 19:40:401541vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161542{
1543 if (capacity() > size())
1544 {
1545#ifndef _LIBCPP_NO_EXCEPTIONS
1546 try
1547 {
Howard Hinnant324bb032010-08-22 00:02:431548#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161549 allocator_type& __a = this->__alloc();
Howard Hinnantd1d27a42011-06-03 19:40:401550 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
Howard Hinnantbc8d3f92010-05-11 19:42:161551 __swap_out_circular_buffer(__v);
1552#ifndef _LIBCPP_NO_EXCEPTIONS
1553 }
1554 catch (...)
1555 {
1556 }
Howard Hinnant324bb032010-08-22 00:02:431557#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161558 }
1559}
1560
1561template <class _Tp, class _Allocator>
Howard Hinnantb0bfd9b2012-02-15 00:41:341562template <class _Up>
1563void
1564#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1565vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1566#else
1567vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1568#endif
1569{
1570 allocator_type& __a = this->__alloc();
1571 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1572 // __v.push_back(_VSTD::forward<_Up>(__x));
Howard Hinnantf619e232013-01-11 20:36:591573 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
1574 __v.__end_++;
Howard Hinnantb0bfd9b2012-02-15 00:41:341575 __swap_out_circular_buffer(__v);
1576}
1577
1578template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001579inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161580void
1581vector<_Tp, _Allocator>::push_back(const_reference __x)
1582{
Howard Hinnantb0bfd9b2012-02-15 00:41:341583 if (this->__end_ != this->__end_cap())
Howard Hinnantbc8d3f92010-05-11 19:42:161584 {
Marshall Clow1f50f2d2014-05-08 14:14:061585 __annotate_increase(1);
Howard Hinnantbc8d3f92010-05-11 19:42:161586 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191587 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161588 ++this->__end_;
1589 }
1590 else
Howard Hinnantb0bfd9b2012-02-15 00:41:341591 __push_back_slow_path(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:161592}
1593
Howard Hinnant73d21a42010-09-04 23:28:191594#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161595
1596template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001597inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161598void
1599vector<_Tp, _Allocator>::push_back(value_type&& __x)
1600{
1601 if (this->__end_ < this->__end_cap())
1602 {
Marshall Clow1f50f2d2014-05-08 14:14:061603 __annotate_increase(1);
Howard Hinnantbc8d3f92010-05-11 19:42:161604 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191605 _VSTD::__to_raw_pointer(this->__end_),
1606 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161607 ++this->__end_;
1608 }
1609 else
Howard Hinnantb0bfd9b2012-02-15 00:41:341610 __push_back_slow_path(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161611}
1612
Howard Hinnant73d21a42010-09-04 23:28:191613#ifndef _LIBCPP_HAS_NO_VARIADICS
1614
Howard Hinnantbc8d3f92010-05-11 19:42:161615template <class _Tp, class _Allocator>
1616template <class... _Args>
1617void
Howard Hinnant0438ea22012-02-26 15:30:121618vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args)
1619{
1620 allocator_type& __a = this->__alloc();
1621 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1622// __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantf619e232013-01-11 20:36:591623 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...);
1624 __v.__end_++;
Howard Hinnant0438ea22012-02-26 15:30:121625 __swap_out_circular_buffer(__v);
1626}
1627
1628template <class _Tp, class _Allocator>
1629template <class... _Args>
Howard Hinnant1e564242013-10-04 22:09:001630inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0438ea22012-02-26 15:30:121631void
Howard Hinnantbc8d3f92010-05-11 19:42:161632vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1633{
1634 if (this->__end_ < this->__end_cap())
1635 {
Marshall Clow1f50f2d2014-05-08 14:14:061636 __annotate_increase(1);
Howard Hinnantbc8d3f92010-05-11 19:42:161637 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191638 _VSTD::__to_raw_pointer(this->__end_),
1639 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161640 ++this->__end_;
1641 }
1642 else
Howard Hinnant0438ea22012-02-26 15:30:121643 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161644}
1645
Howard Hinnant73d21a42010-09-04 23:28:191646#endif // _LIBCPP_HAS_NO_VARIADICS
1647#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161648
1649template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001650inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161651void
1652vector<_Tp, _Allocator>::pop_back()
1653{
Howard Hinnant7a563db2011-09-14 18:33:511654 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector");
Howard Hinnantbc8d3f92010-05-11 19:42:161655 this->__destruct_at_end(this->__end_ - 1);
1656}
1657
1658template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:001659inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161660typename vector<_Tp, _Allocator>::iterator
1661vector<_Tp, _Allocator>::erase(const_iterator __position)
1662{
Howard Hinnantabe26282011-09-16 17:29:171663#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511664 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1665 "vector::erase(iterator) called with an iterator not"
1666 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171667#endif
Howard Hinnant782da332013-03-25 22:12:261668 _LIBCPP_ASSERT(__position != end(),
1669 "vector::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnant2c39cbe2013-06-27 19:35:321670 difference_type __ps = __position - cbegin();
1671 pointer __p = this->__begin_ + __ps;
Howard Hinnantbc8d3f92010-05-11 19:42:161672 iterator __r = __make_iter(__p);
Howard Hinnant0949eed2011-06-30 21:18:191673 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:161674 return __r;
1675}
1676
1677template <class _Tp, class _Allocator>
1678typename vector<_Tp, _Allocator>::iterator
1679vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
1680{
Howard Hinnantabe26282011-09-16 17:29:171681#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511682 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
1683 "vector::erase(iterator, iterator) called with an iterator not"
1684 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171685#endif
Howard Hinnant7a563db2011-09-14 18:33:511686 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:161687 pointer __p = this->__begin_ + (__first - begin());
1688 iterator __r = __make_iter(__p);
Howard Hinnantb4e67cf2013-04-18 15:02:571689 if (__first != __last)
1690 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
Howard Hinnantbc8d3f92010-05-11 19:42:161691 return __r;
1692}
1693
1694template <class _Tp, class _Allocator>
1695void
1696vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to)
1697{
1698 pointer __old_last = this->__end_;
1699 difference_type __n = __old_last - __to;
1700 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_)
1701 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191702 _VSTD::__to_raw_pointer(this->__end_),
1703 _VSTD::move(*__i));
1704 _VSTD::move_backward(__from_s, __from_s + __n, __old_last);
Howard Hinnantbc8d3f92010-05-11 19:42:161705}
1706
1707template <class _Tp, class _Allocator>
1708typename vector<_Tp, _Allocator>::iterator
1709vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
1710{
Howard Hinnantabe26282011-09-16 17:29:171711#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511712 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1713 "vector::insert(iterator, x) called with an iterator not"
1714 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171715#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161716 pointer __p = this->__begin_ + (__position - begin());
1717 if (this->__end_ < this->__end_cap())
1718 {
Marshall Clow1f50f2d2014-05-08 14:14:061719 __annotate_increase(1);
Howard Hinnantbc8d3f92010-05-11 19:42:161720 if (__p == this->__end_)
1721 {
1722 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191723 _VSTD::__to_raw_pointer(this->__end_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:161724 ++this->__end_;
1725 }
1726 else
1727 {
1728 __move_range(__p, this->__end_, __p + 1);
1729 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1730 if (__p <= __xr && __xr < this->__end_)
1731 ++__xr;
1732 *__p = *__xr;
1733 }
1734 }
1735 else
1736 {
1737 allocator_type& __a = this->__alloc();
1738 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
1739 __v.push_back(__x);
1740 __p = __swap_out_circular_buffer(__v, __p);
1741 }
1742 return __make_iter(__p);
1743}
1744
Howard Hinnant73d21a42010-09-04 23:28:191745#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161746
1747template <class _Tp, class _Allocator>
1748typename vector<_Tp, _Allocator>::iterator
1749vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
1750{
Howard Hinnantabe26282011-09-16 17:29:171751#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511752 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1753 "vector::insert(iterator, x) called with an iterator not"
1754 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171755#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161756 pointer __p = this->__begin_ + (__position - begin());
1757 if (this->__end_ < this->__end_cap())
1758 {
Marshall Clow1f50f2d2014-05-08 14:14:061759 __annotate_increase(1);
Howard Hinnantbc8d3f92010-05-11 19:42:161760 if (__p == this->__end_)
1761 {
1762 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191763 _VSTD::__to_raw_pointer(this->__end_),
1764 _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161765 ++this->__end_;
1766 }
1767 else
1768 {
1769 __move_range(__p, this->__end_, __p + 1);
Howard Hinnant0949eed2011-06-30 21:18:191770 *__p = _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:161771 }
1772 }
1773 else
1774 {
1775 allocator_type& __a = this->__alloc();
1776 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:191777 __v.push_back(_VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:161778 __p = __swap_out_circular_buffer(__v, __p);
1779 }
1780 return __make_iter(__p);
1781}
1782
Howard Hinnant73d21a42010-09-04 23:28:191783#ifndef _LIBCPP_HAS_NO_VARIADICS
1784
Howard Hinnantbc8d3f92010-05-11 19:42:161785template <class _Tp, class _Allocator>
1786template <class... _Args>
1787typename vector<_Tp, _Allocator>::iterator
1788vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
1789{
Howard Hinnantabe26282011-09-16 17:29:171790#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511791 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1792 "vector::emplace(iterator, x) called with an iterator not"
1793 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171794#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161795 pointer __p = this->__begin_ + (__position - begin());
1796 if (this->__end_ < this->__end_cap())
1797 {
Marshall Clow1f50f2d2014-05-08 14:14:061798 __annotate_increase(1);
Howard Hinnantbc8d3f92010-05-11 19:42:161799 if (__p == this->__end_)
1800 {
1801 __alloc_traits::construct(this->__alloc(),
Howard Hinnant0949eed2011-06-30 21:18:191802 _VSTD::__to_raw_pointer(this->__end_),
1803 _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161804 ++this->__end_;
1805 }
1806 else
1807 {
Howard Hinnanta58402a2012-07-08 23:23:041808 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161809 __move_range(__p, this->__end_, __p + 1);
Howard Hinnanta58402a2012-07-08 23:23:041810 *__p = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:161811 }
1812 }
1813 else
1814 {
1815 allocator_type& __a = this->__alloc();
1816 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a);
Howard Hinnant0949eed2011-06-30 21:18:191817 __v.emplace_back(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:161818 __p = __swap_out_circular_buffer(__v, __p);
1819 }
1820 return __make_iter(__p);
1821}
1822
Howard Hinnant73d21a42010-09-04 23:28:191823#endif // _LIBCPP_HAS_NO_VARIADICS
1824#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:161825
1826template <class _Tp, class _Allocator>
1827typename vector<_Tp, _Allocator>::iterator
1828vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
1829{
Howard Hinnantabe26282011-09-16 17:29:171830#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511831 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1832 "vector::insert(iterator, n, x) called with an iterator not"
1833 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171834#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161835 pointer __p = this->__begin_ + (__position - begin());
1836 if (__n > 0)
1837 {
1838 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_))
1839 {
1840 size_type __old_n = __n;
1841 pointer __old_last = this->__end_;
1842 if (__n > static_cast<size_type>(this->__end_ - __p))
1843 {
1844 size_type __cx = __n - (this->__end_ - __p);
1845 __construct_at_end(__cx, __x);
1846 __n -= __cx;
1847 }
1848 if (__n > 0)
1849 {
Marshall Clow1f50f2d2014-05-08 14:14:061850 __annotate_increase(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:161851 __move_range(__p, __old_last, __p + __old_n);
1852 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1853 if (__p <= __xr && __xr < this->__end_)
1854 __xr += __old_n;
Howard Hinnant0949eed2011-06-30 21:18:191855 _VSTD::fill_n(__p, __n, *__xr);
Howard Hinnantbc8d3f92010-05-11 19:42:161856 }
1857 }
1858 else
1859 {
1860 allocator_type& __a = this->__alloc();
1861 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1862 __v.__construct_at_end(__n, __x);
1863 __p = __swap_out_circular_buffer(__v, __p);
1864 }
1865 }
1866 return __make_iter(__p);
1867}
1868
1869template <class _Tp, class _Allocator>
1870template <class _InputIterator>
1871typename enable_if
1872<
1873 __is_input_iterator <_InputIterator>::value &&
Howard Hinnant742fecb2013-03-28 17:44:321874 !__is_forward_iterator<_InputIterator>::value &&
1875 is_constructible<
1876 _Tp,
1877 typename iterator_traits<_InputIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161878 typename vector<_Tp, _Allocator>::iterator
1879>::type
1880vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
1881{
Howard Hinnantabe26282011-09-16 17:29:171882#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511883 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1884 "vector::insert(iterator, range) called with an iterator not"
1885 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171886#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161887 difference_type __off = __position - begin();
1888 pointer __p = this->__begin_ + __off;
1889 allocator_type& __a = this->__alloc();
1890 pointer __old_last = this->__end_;
1891 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first)
1892 {
Howard Hinnant0949eed2011-06-30 21:18:191893 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_),
Howard Hinnantbc8d3f92010-05-11 19:42:161894 *__first);
1895 ++this->__end_;
1896 }
1897 __split_buffer<value_type, allocator_type&> __v(__a);
1898 if (__first != __last)
1899 {
1900#ifndef _LIBCPP_NO_EXCEPTIONS
1901 try
1902 {
Howard Hinnant324bb032010-08-22 00:02:431903#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161904 __v.__construct_at_end(__first, __last);
1905 difference_type __old_size = __old_last - this->__begin_;
1906 difference_type __old_p = __p - this->__begin_;
1907 reserve(__recommend(size() + __v.size()));
1908 __p = this->__begin_ + __old_p;
1909 __old_last = this->__begin_ + __old_size;
1910#ifndef _LIBCPP_NO_EXCEPTIONS
1911 }
1912 catch (...)
1913 {
1914 erase(__make_iter(__old_last), end());
1915 throw;
1916 }
Howard Hinnant324bb032010-08-22 00:02:431917#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161918 }
Howard Hinnant0949eed2011-06-30 21:18:191919 __p = _VSTD::rotate(__p, __old_last, this->__end_);
Howard Hinnant0442b122011-09-16 18:41:291920 insert(__make_iter(__p), make_move_iterator(__v.begin()),
1921 make_move_iterator(__v.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:161922 return begin() + __off;
1923}
1924
1925template <class _Tp, class _Allocator>
1926template <class _ForwardIterator>
1927typename enable_if
1928<
Howard Hinnant742fecb2013-03-28 17:44:321929 __is_forward_iterator<_ForwardIterator>::value &&
1930 is_constructible<
1931 _Tp,
1932 typename iterator_traits<_ForwardIterator>::reference>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:161933 typename vector<_Tp, _Allocator>::iterator
1934>::type
1935vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
1936{
Howard Hinnantabe26282011-09-16 17:29:171937#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:511938 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,
1939 "vector::insert(iterator, range) called with an iterator not"
1940 " referring to this vector");
Howard Hinnantabe26282011-09-16 17:29:171941#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161942 pointer __p = this->__begin_ + (__position - begin());
Howard Hinnant0949eed2011-06-30 21:18:191943 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:161944 if (__n > 0)
1945 {
1946 if (__n <= this->__end_cap() - this->__end_)
1947 {
1948 size_type __old_n = __n;
1949 pointer __old_last = this->__end_;
1950 _ForwardIterator __m = __last;
1951 difference_type __dx = this->__end_ - __p;
1952 if (__n > __dx)
1953 {
1954 __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:191955 _VSTD::advance(__m, this->__end_ - __p);
Howard Hinnantbc8d3f92010-05-11 19:42:161956 __construct_at_end(__m, __last);
1957 __n = __dx;
1958 }
1959 if (__n > 0)
1960 {
Marshall Clow1f50f2d2014-05-08 14:14:061961 __annotate_increase(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:161962 __move_range(__p, __old_last, __p + __old_n);
Howard Hinnant0949eed2011-06-30 21:18:191963 _VSTD::copy(__first, __m, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:161964 }
1965 }
1966 else
1967 {
1968 allocator_type& __a = this->__alloc();
1969 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a);
1970 __v.__construct_at_end(__first, __last);
1971 __p = __swap_out_circular_buffer(__v, __p);
1972 }
1973 }
1974 return __make_iter(__p);
1975}
1976
1977template <class _Tp, class _Allocator>
1978void
1979vector<_Tp, _Allocator>::resize(size_type __sz)
1980{
1981 size_type __cs = size();
1982 if (__cs < __sz)
1983 this->__append(__sz - __cs);
1984 else if (__cs > __sz)
1985 this->__destruct_at_end(this->__begin_ + __sz);
1986}
1987
1988template <class _Tp, class _Allocator>
1989void
1990vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x)
1991{
1992 size_type __cs = size();
1993 if (__cs < __sz)
1994 this->__append(__sz - __cs, __x);
1995 else if (__cs > __sz)
1996 this->__destruct_at_end(this->__begin_ + __sz);
1997}
1998
1999template <class _Tp, class _Allocator>
2000void
2001vector<_Tp, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:402002 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2003 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162004{
Howard Hinnant7a563db2011-09-14 18:33:512005 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value ||
2006 this->__alloc() == __x.__alloc(),
2007 "vector::swap: Either propagate_on_container_swap must be true"
2008 " or the allocators must compare equal");
Howard Hinnant0949eed2011-06-30 21:18:192009 _VSTD::swap(this->__begin_, __x.__begin_);
2010 _VSTD::swap(this->__end_, __x.__end_);
2011 _VSTD::swap(this->__end_cap(), __x.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:162012 __base::__swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantabe26282011-09-16 17:29:172013#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512014 __get_db()->swap(this, &__x);
Howard Hinnantabe26282011-09-16 17:29:172015#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:162016}
2017
Howard Hinnant324bb032010-08-22 00:02:432018template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162019bool
2020vector<_Tp, _Allocator>::__invariants() const
2021{
Howard Hinnant2c39cbe2013-06-27 19:35:322022 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162023 {
Howard Hinnant2c39cbe2013-06-27 19:35:322024 if (this->__end_ != nullptr || this->__end_cap() != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162025 return false;
2026 }
2027 else
2028 {
2029 if (this->__begin_ > this->__end_)
2030 return false;
2031 if (this->__begin_ == this->__end_cap())
2032 return false;
2033 if (this->__end_ > this->__end_cap())
2034 return false;
2035 }
2036 return true;
2037}
2038
Howard Hinnantabe26282011-09-16 17:29:172039#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512040
Howard Hinnantbc8d3f92010-05-11 19:42:162041template <class _Tp, class _Allocator>
Howard Hinnant7a563db2011-09-14 18:33:512042bool
2043vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const
2044{
2045 return this->__begin_ <= __i->base() && __i->base() < this->__end_;
2046}
2047
2048template <class _Tp, class _Allocator>
2049bool
2050vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const
2051{
2052 return this->__begin_ < __i->base() && __i->base() <= this->__end_;
2053}
2054
2055template <class _Tp, class _Allocator>
2056bool
2057vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
2058{
2059 const_pointer __p = __i->base() + __n;
2060 return this->__begin_ <= __p && __p <= this->__end_;
2061}
2062
2063template <class _Tp, class _Allocator>
2064bool
2065vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
2066{
2067 const_pointer __p = __i->base() + __n;
2068 return this->__begin_ <= __p && __p < this->__end_;
2069}
2070
Howard Hinnantabe26282011-09-16 17:29:172071#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512072
2073template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002074inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162075void
2076vector<_Tp, _Allocator>::__invalidate_all_iterators()
2077{
Howard Hinnantabe26282011-09-16 17:29:172078#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant7a563db2011-09-14 18:33:512079 __get_db()->__invalidate_all(this);
Howard Hinnantabe26282011-09-16 17:29:172080#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:162081}
2082
2083// vector<bool>
2084
2085template <class _Allocator> class vector<bool, _Allocator>;
2086
2087template <class _Allocator> struct hash<vector<bool, _Allocator> >;
2088
2089template <class _Allocator>
Howard Hinnantf03c3b42011-07-02 20:33:232090struct __has_storage_type<vector<bool, _Allocator> >
2091{
2092 static const bool value = true;
2093};
2094
2095template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:342096class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162097 : private __vector_base_common<true>
2098{
2099public:
2100 typedef vector __self;
Howard Hinnant324bb032010-08-22 00:02:432101 typedef bool value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:162102 typedef _Allocator allocator_type;
2103 typedef allocator_traits<allocator_type> __alloc_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:162104 typedef typename __alloc_traits::size_type size_type;
2105 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantf867f632012-05-07 16:50:382106 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:162107 typedef __bit_iterator<vector, false> pointer;
2108 typedef __bit_iterator<vector, true> const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:162109 typedef pointer iterator;
2110 typedef const_pointer const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:192111 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
2112 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:162113
2114private:
Howard Hinnantbc8d3f92010-05-11 19:42:162115 typedef typename __alloc_traits::template
2116#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
2117 rebind_alloc<__storage_type>
2118#else
2119 rebind_alloc<__storage_type>::other
2120#endif
2121 __storage_allocator;
2122 typedef allocator_traits<__storage_allocator> __storage_traits;
2123 typedef typename __storage_traits::pointer __storage_pointer;
2124 typedef typename __storage_traits::const_pointer __const_storage_pointer;
2125
2126 __storage_pointer __begin_;
2127 size_type __size_;
2128 __compressed_pair<size_type, __storage_allocator> __cap_alloc_;
Howard Hinnant2bf1c082011-07-09 15:50:422129public:
Howard Hinnantf03c3b42011-07-02 20:33:232130 typedef __bit_reference<vector> reference;
2131 typedef __bit_const_reference<vector> const_reference;
Howard Hinnant2bf1c082011-07-09 15:50:422132private:
Howard Hinnantd1d27a42011-06-03 19:40:402133 _LIBCPP_INLINE_VISIBILITY
2134 size_type& __cap() _NOEXCEPT
2135 {return __cap_alloc_.first();}
2136 _LIBCPP_INLINE_VISIBILITY
2137 const size_type& __cap() const _NOEXCEPT
2138 {return __cap_alloc_.first();}
2139 _LIBCPP_INLINE_VISIBILITY
2140 __storage_allocator& __alloc() _NOEXCEPT
2141 {return __cap_alloc_.second();}
2142 _LIBCPP_INLINE_VISIBILITY
2143 const __storage_allocator& __alloc() const _NOEXCEPT
2144 {return __cap_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:162145
2146 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
2147
Howard Hinnantd1d27a42011-06-03 19:40:402148 _LIBCPP_INLINE_VISIBILITY
2149 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162150 {return __n * __bits_per_word;}
Howard Hinnantd1d27a42011-06-03 19:40:402151 _LIBCPP_INLINE_VISIBILITY
2152 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162153 {return (__n - 1) / __bits_per_word + 1;}
2154
2155public:
Howard Hinnantd1d27a42011-06-03 19:40:402156 _LIBCPP_INLINE_VISIBILITY
2157 vector()
2158 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:432159 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:162160 ~vector();
2161 explicit vector(size_type __n);
Marshall Clowa49a2c92013-09-14 00:47:592162#if _LIBCPP_STD_VER > 11
2163 explicit vector(size_type __n, const allocator_type& __a);
2164#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162165 vector(size_type __n, const value_type& __v);
2166 vector(size_type __n, const value_type& __v, const allocator_type& __a);
2167 template <class _InputIterator>
2168 vector(_InputIterator __first, _InputIterator __last,
2169 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2170 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2171 template <class _InputIterator>
2172 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2173 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2174 !__is_forward_iterator<_InputIterator>::value>::type* = 0);
2175 template <class _ForwardIterator>
2176 vector(_ForwardIterator __first, _ForwardIterator __last,
2177 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2178 template <class _ForwardIterator>
2179 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2180 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0);
2181
2182 vector(const vector& __v);
2183 vector(const vector& __v, const allocator_type& __a);
2184 vector& operator=(const vector& __v);
Howard Hinnante3e32912011-08-12 21:56:022185#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162186 vector(initializer_list<value_type> __il);
2187 vector(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:022188#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162189
Howard Hinnant73d21a42010-09-04 23:28:192190#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantd1d27a42011-06-03 19:40:402191 _LIBCPP_INLINE_VISIBILITY
2192 vector(vector&& __v)
2193 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:162194 vector(vector&& __v, const allocator_type& __a);
Howard Hinnantd1d27a42011-06-03 19:40:402195 _LIBCPP_INLINE_VISIBILITY
2196 vector& operator=(vector&& __v)
2197 _NOEXCEPT_(
2198 __alloc_traits::propagate_on_container_move_assignment::value &&
2199 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:192200#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:022201#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:282202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162203 vector& operator=(initializer_list<value_type> __il)
2204 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:022205#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162206
2207 template <class _InputIterator>
2208 typename enable_if
2209 <
2210 __is_input_iterator<_InputIterator>::value &&
2211 !__is_forward_iterator<_InputIterator>::value,
2212 void
2213 >::type
2214 assign(_InputIterator __first, _InputIterator __last);
2215 template <class _ForwardIterator>
2216 typename enable_if
2217 <
2218 __is_forward_iterator<_ForwardIterator>::value,
2219 void
2220 >::type
2221 assign(_ForwardIterator __first, _ForwardIterator __last);
2222
2223 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:022224#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:282225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162226 void assign(initializer_list<value_type> __il)
2227 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:022228#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162229
Howard Hinnantd1d27a42011-06-03 19:40:402230 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162231 {return allocator_type(this->__alloc());}
2232
Howard Hinnantd1d27a42011-06-03 19:40:402233 size_type max_size() const _NOEXCEPT;
2234 _LIBCPP_INLINE_VISIBILITY
2235 size_type capacity() const _NOEXCEPT
2236 {return __internal_cap_to_external(__cap());}
2237 _LIBCPP_INLINE_VISIBILITY
2238 size_type size() const _NOEXCEPT
2239 {return __size_;}
2240 _LIBCPP_INLINE_VISIBILITY
2241 bool empty() const _NOEXCEPT
2242 {return __size_ == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:162243 void reserve(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:402244 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162245
Howard Hinnantd1d27a42011-06-03 19:40:402246 _LIBCPP_INLINE_VISIBILITY
2247 iterator begin() _NOEXCEPT
2248 {return __make_iter(0);}
2249 _LIBCPP_INLINE_VISIBILITY
2250 const_iterator begin() const _NOEXCEPT
2251 {return __make_iter(0);}
2252 _LIBCPP_INLINE_VISIBILITY
2253 iterator end() _NOEXCEPT
2254 {return __make_iter(__size_);}
2255 _LIBCPP_INLINE_VISIBILITY
2256 const_iterator end() const _NOEXCEPT
2257 {return __make_iter(__size_);}
Howard Hinnantbc8d3f92010-05-11 19:42:162258
Howard Hinnantd1d27a42011-06-03 19:40:402259 _LIBCPP_INLINE_VISIBILITY
2260 reverse_iterator rbegin() _NOEXCEPT
2261 {return reverse_iterator(end());}
2262 _LIBCPP_INLINE_VISIBILITY
2263 const_reverse_iterator rbegin() const _NOEXCEPT
2264 {return const_reverse_iterator(end());}
2265 _LIBCPP_INLINE_VISIBILITY
2266 reverse_iterator rend() _NOEXCEPT
2267 {return reverse_iterator(begin());}
2268 _LIBCPP_INLINE_VISIBILITY
2269 const_reverse_iterator rend() const _NOEXCEPT
2270 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:162271
Howard Hinnantd1d27a42011-06-03 19:40:402272 _LIBCPP_INLINE_VISIBILITY
2273 const_iterator cbegin() const _NOEXCEPT
2274 {return __make_iter(0);}
2275 _LIBCPP_INLINE_VISIBILITY
2276 const_iterator cend() const _NOEXCEPT
2277 {return __make_iter(__size_);}
2278 _LIBCPP_INLINE_VISIBILITY
2279 const_reverse_iterator crbegin() const _NOEXCEPT
2280 {return rbegin();}
2281 _LIBCPP_INLINE_VISIBILITY
2282 const_reverse_iterator crend() const _NOEXCEPT
2283 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:162284
2285 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);}
2286 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);}
2287 reference at(size_type __n);
2288 const_reference at(size_type __n) const;
2289
2290 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);}
2291 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);}
2292 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);}
2293 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);}
2294
2295 void push_back(const value_type& __x);
Marshall Clow198a2a52013-08-13 23:54:122296#if _LIBCPP_STD_VER > 11
2297 template <class... _Args>
2298 _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
2299 { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); }
2300#endif
2301
Howard Hinnantbc8d3f92010-05-11 19:42:162302 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;}
2303
Marshall Clow198a2a52013-08-13 23:54:122304#if _LIBCPP_STD_VER > 11
2305 template <class... _Args>
2306 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args)
2307 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); }
2308#endif
2309
Howard Hinnantbc8d3f92010-05-11 19:42:162310 iterator insert(const_iterator __position, const value_type& __x);
2311 iterator insert(const_iterator __position, size_type __n, const value_type& __x);
2312 iterator insert(const_iterator __position, size_type __n, const_reference __x);
2313 template <class _InputIterator>
2314 typename enable_if
2315 <
2316 __is_input_iterator <_InputIterator>::value &&
2317 !__is_forward_iterator<_InputIterator>::value,
2318 iterator
2319 >::type
2320 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
2321 template <class _ForwardIterator>
2322 typename enable_if
2323 <
2324 __is_forward_iterator<_ForwardIterator>::value,
2325 iterator
2326 >::type
2327 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:022328#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantee6ccd02010-09-23 18:58:282329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162330 iterator insert(const_iterator __position, initializer_list<value_type> __il)
2331 {return insert(__position, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:022332#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:162333
Howard Hinnant2d72b1e2010-12-17 14:46:432334 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position);
Howard Hinnantbc8d3f92010-05-11 19:42:162335 iterator erase(const_iterator __first, const_iterator __last);
2336
Howard Hinnantd1d27a42011-06-03 19:40:402337 _LIBCPP_INLINE_VISIBILITY
2338 void clear() _NOEXCEPT {__size_ = 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:162339
Howard Hinnantd1d27a42011-06-03 19:40:402340 void swap(vector&)
2341 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2342 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:162343
2344 void resize(size_type __sz, value_type __x = false);
Howard Hinnantd1d27a42011-06-03 19:40:402345 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162346
2347 bool __invariants() const;
2348
2349private:
Howard Hinnant2d72b1e2010-12-17 14:46:432350 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:162351 void allocate(size_type __n);
Howard Hinnantd1d27a42011-06-03 19:40:402352 void deallocate() _NOEXCEPT;
2353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:202354 static size_type __align_it(size_type __new_size) _NOEXCEPT
Marshall Clow0d1965d2014-07-28 15:02:422355 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);};
Howard Hinnant2d72b1e2010-12-17 14:46:432356 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const;
2357 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162358 template <class _ForwardIterator>
2359 typename enable_if
2360 <
2361 __is_forward_iterator<_ForwardIterator>::value,
2362 void
2363 >::type
2364 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
2365 void __append(size_type __n, const_reference __x);
Howard Hinnantd1d27a42011-06-03 19:40:402366 _LIBCPP_INLINE_VISIBILITY
2367 reference __make_ref(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162368 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:402369 _LIBCPP_INLINE_VISIBILITY
2370 const_reference __make_ref(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162371 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd1d27a42011-06-03 19:40:402372 _LIBCPP_INLINE_VISIBILITY
2373 iterator __make_iter(size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162374 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:402375 _LIBCPP_INLINE_VISIBILITY
2376 const_iterator __make_iter(size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162377 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));}
Howard Hinnantd1d27a42011-06-03 19:40:402378 _LIBCPP_INLINE_VISIBILITY
2379 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT
Howard Hinnant2c39cbe2013-06-27 19:35:322380 {return begin() + (__p - cbegin());}
Howard Hinnantbc8d3f92010-05-11 19:42:162381
Howard Hinnantee6ccd02010-09-23 18:58:282382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162383 void __copy_assign_alloc(const vector& __v)
2384 {__copy_assign_alloc(__v, integral_constant<bool,
2385 __storage_traits::propagate_on_container_copy_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:282386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162387 void __copy_assign_alloc(const vector& __c, true_type)
2388 {
2389 if (__alloc() != __c.__alloc())
2390 deallocate();
2391 __alloc() = __c.__alloc();
2392 }
2393
Howard Hinnantee6ccd02010-09-23 18:58:282394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042395 void __copy_assign_alloc(const vector&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:162396 {}
2397
2398 void __move_assign(vector& __c, false_type);
Howard Hinnantd1d27a42011-06-03 19:40:402399 void __move_assign(vector& __c, true_type)
2400 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantee6ccd02010-09-23 18:58:282401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162402 void __move_assign_alloc(vector& __c)
Howard Hinnantd1d27a42011-06-03 19:40:402403 _NOEXCEPT_(
2404 !__storage_traits::propagate_on_container_move_assignment::value ||
2405 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162406 {__move_assign_alloc(__c, integral_constant<bool,
2407 __storage_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnantee6ccd02010-09-23 18:58:282408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:312409 void __move_assign_alloc(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402410 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162411 {
Howard Hinnant0949eed2011-06-30 21:18:192412 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:162413 }
2414
Howard Hinnantee6ccd02010-09-23 18:58:282415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042416 void __move_assign_alloc(vector&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:402417 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162418 {}
2419
Howard Hinnantee6ccd02010-09-23 18:58:282420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162421 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y)
Howard Hinnantd1d27a42011-06-03 19:40:402422 _NOEXCEPT_(
2423 !__storage_traits::propagate_on_container_swap::value ||
2424 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162425 {__swap_alloc(__x, __y, integral_constant<bool,
2426 __storage_traits::propagate_on_container_swap::value>());}
2427
Howard Hinnantee6ccd02010-09-23 18:58:282428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162429 static void __swap_alloc(__storage_allocator& __x, __storage_allocator& __y, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402430 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162431 {
Howard Hinnant0949eed2011-06-30 21:18:192432 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:162433 swap(__x, __y);
2434 }
Howard Hinnantee6ccd02010-09-23 18:58:282435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:042436 static void __swap_alloc(__storage_allocator&, __storage_allocator&, false_type)
Howard Hinnantd1d27a42011-06-03 19:40:402437 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162438 {}
2439
Howard Hinnantd1d27a42011-06-03 19:40:402440 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:162441
2442 friend class __bit_reference<vector>;
2443 friend class __bit_const_reference<vector>;
2444 friend class __bit_iterator<vector, false>;
2445 friend class __bit_iterator<vector, true>;
Howard Hinnant4ae952a2012-08-17 17:10:182446 friend struct __bit_array<vector>;
Howard Hinnant0f678bd2013-08-12 18:38:342447 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>;
Howard Hinnantbc8d3f92010-05-11 19:42:162448};
2449
2450template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002451inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162452void
2453vector<bool, _Allocator>::__invalidate_all_iterators()
2454{
Howard Hinnantbc8d3f92010-05-11 19:42:162455}
2456
2457// Allocate space for __n objects
2458// throws length_error if __n > max_size()
2459// throws (probably bad_alloc) if memory run out
2460// Precondition: __begin_ == __end_ == __cap() == 0
2461// Precondition: __n > 0
2462// Postcondition: capacity() == __n
2463// Postcondition: size() == 0
2464template <class _Allocator>
2465void
2466vector<bool, _Allocator>::allocate(size_type __n)
2467{
2468 if (__n > max_size())
2469 this->__throw_length_error();
2470 __n = __external_cap_to_internal(__n);
2471 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n);
2472 this->__size_ = 0;
2473 this->__cap() = __n;
2474}
2475
2476template <class _Allocator>
2477void
Howard Hinnantd1d27a42011-06-03 19:40:402478vector<bool, _Allocator>::deallocate() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162479{
Howard Hinnant2c39cbe2013-06-27 19:35:322480 if (this->__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162481 {
2482 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap());
2483 __invalidate_all_iterators();
Howard Hinnant2c39cbe2013-06-27 19:35:322484 this->__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:162485 this->__size_ = this->__cap() = 0;
2486 }
2487}
2488
2489template <class _Allocator>
2490typename vector<bool, _Allocator>::size_type
Howard Hinnantd1d27a42011-06-03 19:40:402491vector<bool, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162492{
2493 size_type __amax = __storage_traits::max_size(__alloc());
2494 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
2495 if (__nmax / __bits_per_word <= __amax)
2496 return __nmax;
2497 return __internal_cap_to_external(__amax);
2498}
2499
2500// Precondition: __new_size > capacity()
2501template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162503typename vector<bool, _Allocator>::size_type
2504vector<bool, _Allocator>::__recommend(size_type __new_size) const
2505{
2506 const size_type __ms = max_size();
2507 if (__new_size > __ms)
2508 this->__throw_length_error();
2509 const size_type __cap = capacity();
2510 if (__cap >= __ms / 2)
2511 return __ms;
Howard Hinnant7f764502013-08-14 18:00:202512 return _VSTD::max(2*__cap, __align_it(__new_size));
Howard Hinnantbc8d3f92010-05-11 19:42:162513}
2514
2515// Default constructs __n objects starting at __end_
2516// Precondition: __n > 0
2517// Precondition: size() + __n <= capacity()
2518// Postcondition: size() == size() + __n
2519template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002520inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162521void
2522vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x)
2523{
2524 size_type __old_size = this->__size_;
2525 this->__size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:192526 _VSTD::fill_n(__make_iter(__old_size), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162527}
2528
2529template <class _Allocator>
2530template <class _ForwardIterator>
2531typename enable_if
2532<
2533 __is_forward_iterator<_ForwardIterator>::value,
2534 void
2535>::type
2536vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
2537{
2538 size_type __old_size = this->__size_;
Howard Hinnant0949eed2011-06-30 21:18:192539 this->__size_ += _VSTD::distance(__first, __last);
2540 _VSTD::copy(__first, __last, __make_iter(__old_size));
Howard Hinnantbc8d3f92010-05-11 19:42:162541}
2542
2543template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002544inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162545vector<bool, _Allocator>::vector()
Howard Hinnantd1d27a42011-06-03 19:40:402546 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant2c39cbe2013-06-27 19:35:322547 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162548 __size_(0),
2549 __cap_alloc_(0)
2550{
2551}
2552
2553template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002554inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162555vector<bool, _Allocator>::vector(const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322556 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162557 __size_(0),
2558 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2559{
2560}
2561
2562template <class _Allocator>
2563vector<bool, _Allocator>::vector(size_type __n)
Howard Hinnant2c39cbe2013-06-27 19:35:322564 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162565 __size_(0),
2566 __cap_alloc_(0)
2567{
2568 if (__n > 0)
2569 {
2570 allocate(__n);
2571 __construct_at_end(__n, false);
2572 }
2573}
2574
Marshall Clowa49a2c92013-09-14 00:47:592575#if _LIBCPP_STD_VER > 11
2576template <class _Allocator>
2577vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
2578 : __begin_(nullptr),
2579 __size_(0),
2580 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2581{
2582 if (__n > 0)
2583 {
2584 allocate(__n);
2585 __construct_at_end(__n, false);
2586 }
2587}
2588#endif
2589
Howard Hinnantbc8d3f92010-05-11 19:42:162590template <class _Allocator>
2591vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
Howard Hinnant2c39cbe2013-06-27 19:35:322592 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162593 __size_(0),
2594 __cap_alloc_(0)
2595{
2596 if (__n > 0)
2597 {
2598 allocate(__n);
2599 __construct_at_end(__n, __x);
2600 }
2601}
2602
2603template <class _Allocator>
2604vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322605 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162606 __size_(0),
2607 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2608{
2609 if (__n > 0)
2610 {
2611 allocate(__n);
2612 __construct_at_end(__n, __x);
2613 }
2614}
2615
2616template <class _Allocator>
2617template <class _InputIterator>
2618vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last,
2619 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2620 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322621 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162622 __size_(0),
2623 __cap_alloc_(0)
2624{
2625#ifndef _LIBCPP_NO_EXCEPTIONS
2626 try
2627 {
Howard Hinnant324bb032010-08-22 00:02:432628#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162629 for (; __first != __last; ++__first)
2630 push_back(*__first);
2631#ifndef _LIBCPP_NO_EXCEPTIONS
2632 }
2633 catch (...)
2634 {
Howard Hinnant2c39cbe2013-06-27 19:35:322635 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162636 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2637 __invalidate_all_iterators();
2638 throw;
2639 }
Howard Hinnant324bb032010-08-22 00:02:432640#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162641}
2642
2643template <class _Allocator>
2644template <class _InputIterator>
2645vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a,
2646 typename enable_if<__is_input_iterator <_InputIterator>::value &&
2647 !__is_forward_iterator<_InputIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322648 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162649 __size_(0),
2650 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2651{
2652#ifndef _LIBCPP_NO_EXCEPTIONS
2653 try
2654 {
Howard Hinnant324bb032010-08-22 00:02:432655#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162656 for (; __first != __last; ++__first)
2657 push_back(*__first);
2658#ifndef _LIBCPP_NO_EXCEPTIONS
2659 }
2660 catch (...)
2661 {
Howard Hinnant2c39cbe2013-06-27 19:35:322662 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162663 __storage_traits::deallocate(__alloc(), __begin_, __cap());
2664 __invalidate_all_iterators();
2665 throw;
2666 }
Howard Hinnant324bb032010-08-22 00:02:432667#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162668}
2669
2670template <class _Allocator>
2671template <class _ForwardIterator>
2672vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last,
2673 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322674 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162675 __size_(0),
2676 __cap_alloc_(0)
2677{
Howard Hinnant0949eed2011-06-30 21:18:192678 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:162679 if (__n > 0)
2680 {
2681 allocate(__n);
2682 __construct_at_end(__first, __last);
2683 }
2684}
2685
2686template <class _Allocator>
2687template <class _ForwardIterator>
2688vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a,
2689 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*)
Howard Hinnant2c39cbe2013-06-27 19:35:322690 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162691 __size_(0),
2692 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2693{
Howard Hinnant0949eed2011-06-30 21:18:192694 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:162695 if (__n > 0)
2696 {
2697 allocate(__n);
2698 __construct_at_end(__first, __last);
2699 }
2700}
2701
Howard Hinnante3e32912011-08-12 21:56:022702#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2703
Howard Hinnantbc8d3f92010-05-11 19:42:162704template <class _Allocator>
2705vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
Howard Hinnant2c39cbe2013-06-27 19:35:322706 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162707 __size_(0),
2708 __cap_alloc_(0)
2709{
2710 size_type __n = static_cast<size_type>(__il.size());
2711 if (__n > 0)
2712 {
2713 allocate(__n);
2714 __construct_at_end(__il.begin(), __il.end());
2715 }
2716}
2717
2718template <class _Allocator>
2719vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322720 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162721 __size_(0),
2722 __cap_alloc_(0, static_cast<__storage_allocator>(__a))
2723{
2724 size_type __n = static_cast<size_type>(__il.size());
2725 if (__n > 0)
2726 {
2727 allocate(__n);
2728 __construct_at_end(__il.begin(), __il.end());
2729 }
2730}
2731
Howard Hinnante3e32912011-08-12 21:56:022732#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2733
Howard Hinnantbc8d3f92010-05-11 19:42:162734template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:162735vector<bool, _Allocator>::~vector()
2736{
Howard Hinnant2c39cbe2013-06-27 19:35:322737 if (__begin_ != nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:162738 __storage_traits::deallocate(__alloc(), __begin_, __cap());
Howard Hinnantbc8d3f92010-05-11 19:42:162739 __invalidate_all_iterators();
Howard Hinnantbc8d3f92010-05-11 19:42:162740}
2741
2742template <class _Allocator>
2743vector<bool, _Allocator>::vector(const vector& __v)
Howard Hinnant2c39cbe2013-06-27 19:35:322744 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162745 __size_(0),
2746 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc()))
2747{
2748 if (__v.size() > 0)
2749 {
2750 allocate(__v.size());
2751 __construct_at_end(__v.begin(), __v.end());
2752 }
2753}
2754
2755template <class _Allocator>
2756vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322757 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162758 __size_(0),
2759 __cap_alloc_(0, __a)
2760{
2761 if (__v.size() > 0)
2762 {
2763 allocate(__v.size());
2764 __construct_at_end(__v.begin(), __v.end());
2765 }
2766}
2767
2768template <class _Allocator>
2769vector<bool, _Allocator>&
2770vector<bool, _Allocator>::operator=(const vector& __v)
2771{
2772 if (this != &__v)
2773 {
2774 __copy_assign_alloc(__v);
2775 if (__v.__size_)
2776 {
2777 if (__v.__size_ > capacity())
2778 {
2779 deallocate();
2780 allocate(__v.__size_);
2781 }
Howard Hinnant0949eed2011-06-30 21:18:192782 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
Howard Hinnantbc8d3f92010-05-11 19:42:162783 }
2784 __size_ = __v.__size_;
2785 }
2786 return *this;
2787}
2788
Howard Hinnant73d21a42010-09-04 23:28:192789#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2790
Howard Hinnantbc8d3f92010-05-11 19:42:162791template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002792inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162793vector<bool, _Allocator>::vector(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:402794 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162795 : __begin_(__v.__begin_),
2796 __size_(__v.__size_),
2797 __cap_alloc_(__v.__cap_alloc_)
2798{
Howard Hinnant2c39cbe2013-06-27 19:35:322799 __v.__begin_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:162800 __v.__size_ = 0;
2801 __v.__cap() = 0;
2802}
2803
2804template <class _Allocator>
2805vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a)
Howard Hinnant2c39cbe2013-06-27 19:35:322806 : __begin_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:162807 __size_(0),
2808 __cap_alloc_(0, __a)
2809{
2810 if (__a == allocator_type(__v.__alloc()))
2811 {
2812 this->__begin_ = __v.__begin_;
2813 this->__size_ = __v.__size_;
2814 this->__cap() = __v.__cap();
2815 __v.__begin_ = nullptr;
2816 __v.__cap() = __v.__size_ = 0;
2817 }
2818 else if (__v.size() > 0)
2819 {
2820 allocate(__v.size());
2821 __construct_at_end(__v.begin(), __v.end());
2822 }
2823}
2824
2825template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:002826inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:162827vector<bool, _Allocator>&
2828vector<bool, _Allocator>::operator=(vector&& __v)
Howard Hinnantd1d27a42011-06-03 19:40:402829 _NOEXCEPT_(
2830 __alloc_traits::propagate_on_container_move_assignment::value &&
2831 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162832{
2833 __move_assign(__v, integral_constant<bool,
2834 __storage_traits::propagate_on_container_move_assignment::value>());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:452835 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:162836}
2837
2838template <class _Allocator>
2839void
2840vector<bool, _Allocator>::__move_assign(vector& __c, false_type)
2841{
2842 if (__alloc() != __c.__alloc())
2843 assign(__c.begin(), __c.end());
2844 else
2845 __move_assign(__c, true_type());
2846}
2847
2848template <class _Allocator>
2849void
2850vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
Howard Hinnantd1d27a42011-06-03 19:40:402851 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:162852{
2853 deallocate();
Marshall Clowdb5e54d2014-07-21 15:15:152854 __move_assign_alloc(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:162855 this->__begin_ = __c.__begin_;
2856 this->__size_ = __c.__size_;
2857 this->__cap() = __c.__cap();
Howard Hinnantbc8d3f92010-05-11 19:42:162858 __c.__begin_ = nullptr;
2859 __c.__cap() = __c.__size_ = 0;
2860}
Howard Hinnant73d21a42010-09-04 23:28:192861
2862#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:162863
2864template <class _Allocator>
2865void
2866vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
2867{
2868 __size_ = 0;
2869 if (__n > 0)
2870 {
2871 size_type __c = capacity();
2872 if (__n <= __c)
2873 __size_ = __n;
2874 else
2875 {
2876 vector __v(__alloc());
2877 __v.reserve(__recommend(__n));
2878 __v.__size_ = __n;
2879 swap(__v);
2880 }
Howard Hinnant0949eed2011-06-30 21:18:192881 _VSTD::fill_n(begin(), __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:162882 }
2883}
2884
2885template <class _Allocator>
2886template <class _InputIterator>
2887typename enable_if
2888<
2889 __is_input_iterator<_InputIterator>::value &&
2890 !__is_forward_iterator<_InputIterator>::value,
2891 void
2892>::type
2893vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2894{
2895 clear();
2896 for (; __first != __last; ++__first)
2897 push_back(*__first);
2898}
2899
2900template <class _Allocator>
2901template <class _ForwardIterator>
2902typename enable_if
2903<
2904 __is_forward_iterator<_ForwardIterator>::value,
2905 void
2906>::type
2907vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2908{
2909 clear();
Howard Hinnant0949eed2011-06-30 21:18:192910 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:162911 if (__n)
2912 {
2913 if (__n > capacity())
2914 {
2915 deallocate();
2916 allocate(__n);
2917 }
2918 __construct_at_end(__first, __last);
2919 }
2920}
2921
2922template <class _Allocator>
2923void
2924vector<bool, _Allocator>::reserve(size_type __n)
2925{
2926 if (__n > capacity())
2927 {
2928 vector __v(this->__alloc());
2929 __v.allocate(__n);
2930 __v.__construct_at_end(this->begin(), this->end());
2931 swap(__v);
2932 __invalidate_all_iterators();
2933 }
2934}
2935
2936template <class _Allocator>
2937void
Howard Hinnantd1d27a42011-06-03 19:40:402938vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162939{
2940 if (__external_cap_to_internal(size()) > __cap())
2941 {
2942#ifndef _LIBCPP_NO_EXCEPTIONS
2943 try
2944 {
Howard Hinnant324bb032010-08-22 00:02:432945#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162946 vector(*this, allocator_type(__alloc())).swap(*this);
2947#ifndef _LIBCPP_NO_EXCEPTIONS
2948 }
2949 catch (...)
2950 {
2951 }
Howard Hinnant324bb032010-08-22 00:02:432952#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:162953 }
2954}
2955
2956template <class _Allocator>
2957typename vector<bool, _Allocator>::reference
2958vector<bool, _Allocator>::at(size_type __n)
2959{
2960 if (__n >= size())
2961 this->__throw_out_of_range();
2962 return (*this)[__n];
2963}
2964
2965template <class _Allocator>
2966typename vector<bool, _Allocator>::const_reference
2967vector<bool, _Allocator>::at(size_type __n) const
2968{
2969 if (__n >= size())
2970 this->__throw_out_of_range();
2971 return (*this)[__n];
2972}
2973
2974template <class _Allocator>
2975void
2976vector<bool, _Allocator>::push_back(const value_type& __x)
2977{
2978 if (this->__size_ == this->capacity())
2979 reserve(__recommend(this->__size_ + 1));
2980 ++this->__size_;
2981 back() = __x;
2982}
2983
2984template <class _Allocator>
2985typename vector<bool, _Allocator>::iterator
2986vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x)
2987{
2988 iterator __r;
2989 if (size() < capacity())
2990 {
2991 const_iterator __old_end = end();
2992 ++__size_;
Howard Hinnant0949eed2011-06-30 21:18:192993 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:162994 __r = __const_iterator_cast(__position);
2995 }
2996 else
2997 {
2998 vector __v(__alloc());
2999 __v.reserve(__recommend(__size_ + 1));
3000 __v.__size_ = __size_ + 1;
Howard Hinnant0949eed2011-06-30 21:18:193001 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3002 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163003 swap(__v);
3004 }
3005 *__r = __x;
3006 return __r;
3007}
3008
3009template <class _Allocator>
3010typename vector<bool, _Allocator>::iterator
3011vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x)
3012{
3013 iterator __r;
3014 size_type __c = capacity();
3015 if (__n <= __c && size() <= __c - __n)
3016 {
3017 const_iterator __old_end = end();
3018 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:193019 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163020 __r = __const_iterator_cast(__position);
3021 }
3022 else
3023 {
3024 vector __v(__alloc());
3025 __v.reserve(__recommend(__size_ + __n));
3026 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193027 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3028 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163029 swap(__v);
3030 }
Howard Hinnant0949eed2011-06-30 21:18:193031 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163032 return __r;
3033}
3034
3035template <class _Allocator>
3036template <class _InputIterator>
3037typename enable_if
3038<
3039 __is_input_iterator <_InputIterator>::value &&
3040 !__is_forward_iterator<_InputIterator>::value,
3041 typename vector<bool, _Allocator>::iterator
3042>::type
3043vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
3044{
3045 difference_type __off = __position - begin();
3046 iterator __p = __const_iterator_cast(__position);
3047 iterator __old_end = end();
3048 for (; size() != capacity() && __first != __last; ++__first)
3049 {
3050 ++this->__size_;
3051 back() = *__first;
3052 }
3053 vector __v(__alloc());
3054 if (__first != __last)
3055 {
3056#ifndef _LIBCPP_NO_EXCEPTIONS
3057 try
3058 {
Howard Hinnant324bb032010-08-22 00:02:433059#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:163060 __v.assign(__first, __last);
3061 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
3062 difference_type __old_p = __p - begin();
3063 reserve(__recommend(size() + __v.size()));
3064 __p = begin() + __old_p;
3065 __old_end = begin() + __old_size;
3066#ifndef _LIBCPP_NO_EXCEPTIONS
3067 }
3068 catch (...)
3069 {
3070 erase(__old_end, end());
3071 throw;
3072 }
Howard Hinnant324bb032010-08-22 00:02:433073#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:163074 }
Howard Hinnant0949eed2011-06-30 21:18:193075 __p = _VSTD::rotate(__p, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163076 insert(__p, __v.begin(), __v.end());
3077 return begin() + __off;
3078}
3079
3080template <class _Allocator>
3081template <class _ForwardIterator>
3082typename enable_if
3083<
3084 __is_forward_iterator<_ForwardIterator>::value,
3085 typename vector<bool, _Allocator>::iterator
3086>::type
3087vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last)
3088{
Howard Hinnant0949eed2011-06-30 21:18:193089 difference_type __n = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:163090 iterator __r;
3091 size_type __c = capacity();
3092 if (__n <= __c && size() <= __c - __n)
3093 {
3094 const_iterator __old_end = end();
3095 __size_ += __n;
Howard Hinnant0949eed2011-06-30 21:18:193096 _VSTD::copy_backward(__position, __old_end, end());
Howard Hinnantbc8d3f92010-05-11 19:42:163097 __r = __const_iterator_cast(__position);
3098 }
3099 else
3100 {
3101 vector __v(__alloc());
3102 __v.reserve(__recommend(__size_ + __n));
3103 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193104 __r = _VSTD::copy(cbegin(), __position, __v.begin());
3105 _VSTD::copy_backward(__position, cend(), __v.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163106 swap(__v);
3107 }
Howard Hinnant0949eed2011-06-30 21:18:193108 _VSTD::copy(__first, __last, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163109 return __r;
3110}
3111
3112template <class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003113inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163114typename vector<bool, _Allocator>::iterator
3115vector<bool, _Allocator>::erase(const_iterator __position)
3116{
3117 iterator __r = __const_iterator_cast(__position);
Howard Hinnant0949eed2011-06-30 21:18:193118 _VSTD::copy(__position + 1, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163119 --__size_;
3120 return __r;
3121}
3122
3123template <class _Allocator>
3124typename vector<bool, _Allocator>::iterator
3125vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last)
3126{
3127 iterator __r = __const_iterator_cast(__first);
3128 difference_type __d = __last - __first;
Howard Hinnant0949eed2011-06-30 21:18:193129 _VSTD::copy(__last, this->cend(), __r);
Howard Hinnantbc8d3f92010-05-11 19:42:163130 __size_ -= __d;
3131 return __r;
3132}
3133
3134template <class _Allocator>
3135void
3136vector<bool, _Allocator>::swap(vector& __x)
Howard Hinnantd1d27a42011-06-03 19:40:403137 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3138 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:163139{
Howard Hinnant0949eed2011-06-30 21:18:193140 _VSTD::swap(this->__begin_, __x.__begin_);
3141 _VSTD::swap(this->__size_, __x.__size_);
3142 _VSTD::swap(this->__cap(), __x.__cap());
Howard Hinnantbc8d3f92010-05-11 19:42:163143 __swap_alloc(this->__alloc(), __x.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:163144}
3145
Howard Hinnant324bb032010-08-22 00:02:433146template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163147void
3148vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
3149{
3150 size_type __cs = size();
3151 if (__cs < __sz)
3152 {
3153 iterator __r;
3154 size_type __c = capacity();
3155 size_type __n = __sz - __cs;
3156 if (__n <= __c && __cs <= __c - __n)
3157 {
3158 __r = end();
3159 __size_ += __n;
3160 }
3161 else
3162 {
3163 vector __v(__alloc());
3164 __v.reserve(__recommend(__size_ + __n));
3165 __v.__size_ = __size_ + __n;
Howard Hinnant0949eed2011-06-30 21:18:193166 __r = _VSTD::copy(cbegin(), cend(), __v.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:163167 swap(__v);
3168 }
Howard Hinnant0949eed2011-06-30 21:18:193169 _VSTD::fill_n(__r, __n, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:163170 }
3171 else
3172 __size_ = __sz;
3173}
3174
Howard Hinnant324bb032010-08-22 00:02:433175template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163176void
Howard Hinnantd1d27a42011-06-03 19:40:403177vector<bool, _Allocator>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163178{
3179 // do middle whole words
3180 size_type __n = __size_;
3181 __storage_pointer __p = __begin_;
3182 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3183 *__p = ~*__p;
3184 // do last partial word
3185 if (__n > 0)
3186 {
3187 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3188 __storage_type __b = *__p & __m;
3189 *__p &= ~__m;
3190 *__p |= ~__b & __m;
3191 }
3192}
3193
Howard Hinnant324bb032010-08-22 00:02:433194template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163195bool
3196vector<bool, _Allocator>::__invariants() const
3197{
Howard Hinnant2c39cbe2013-06-27 19:35:323198 if (this->__begin_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:163199 {
3200 if (this->__size_ != 0 || this->__cap() != 0)
3201 return false;
3202 }
3203 else
3204 {
3205 if (this->__cap() == 0)
3206 return false;
3207 if (this->__size_ > this->capacity())
3208 return false;
3209 }
3210 return true;
3211}
3212
Howard Hinnant324bb032010-08-22 00:02:433213template <class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:163214size_t
Howard Hinnantd1d27a42011-06-03 19:40:403215vector<bool, _Allocator>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163216{
3217 size_t __h = 0;
3218 // do middle whole words
3219 size_type __n = __size_;
3220 __storage_pointer __p = __begin_;
3221 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
3222 __h ^= *__p;
3223 // do last partial word
3224 if (__n > 0)
3225 {
3226 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3227 __h ^= *__p & __m;
3228 }
3229 return __h;
3230}
3231
3232template <class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:343233struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:163234 : public unary_function<vector<bool, _Allocator>, size_t>
3235{
Howard Hinnantee6ccd02010-09-23 18:58:283236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd1d27a42011-06-03 19:40:403237 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:163238 {return __vec.__hash_code();}
3239};
3240
3241template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003242inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163243bool
3244operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3245{
3246 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:193247 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:163248}
3249
3250template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003251inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163252bool
3253operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3254{
3255 return !(__x == __y);
3256}
3257
3258template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163260bool
3261operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3262{
Howard Hinnant0949eed2011-06-30 21:18:193263 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:163264}
3265
3266template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163268bool
3269operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3270{
3271 return __y < __x;
3272}
3273
3274template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163276bool
3277operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3278{
3279 return !(__x < __y);
3280}
3281
3282template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003283inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163284bool
3285operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
3286{
3287 return !(__y < __x);
3288}
3289
3290template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:003291inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:163292void
3293swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
Howard Hinnantd1d27a42011-06-03 19:40:403294 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:163295{
3296 __x.swap(__y);
3297}
3298
3299_LIBCPP_END_NAMESPACE_STD
3300
3301#endif // _LIBCPP_VECTOR